首页 > 数据库 >多线程查询数据库避免重复

多线程查询数据库避免重复

时间:2023-02-07 09:35:00浏览次数:46  
标签:java 重复 org springframework util 查询数据库 import 多线程 datas

contriller:

package batch;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@RestController
public class BatchController {

@Autowired
    User userMapper;
    @Autowired
    BatchServiceImpl batchService;
    int  total = 0;
@RequestMapping("/ThreadBatch")
    public String ThreadBatch(){
        ExecutorService pool = Executors.newFixedThreadPool(10);
        //计数器设置为10个,用来调度主线程和子线程之间关系
        CountDownLatch downLatch = new CountDownLatch(10);
        Object lock = new Object();

        for(int i=0;i<10;i++){
            pool.submit(() -> {
                try {
                    int j = 0;
                    while (true) {
                        //处理逻辑 先去数据库更新1000条数据,再查询这一千条数据
                        //查询数据,若查询出的数据为空,直接break
                        List<JSONObject> datas = new ArrayList<>();
                        datas = batchService.updataSelect(Thread.currentThread().getId(), j);
                        synchronized (lock) {
                            total += datas.size();//验证total和数据总数是否一样
                        }
                        System.out.println("线程" + Thread.currentThread().getId() + "查询到" + datas.size() + "条记录");
                        if (CollectionUtils.isEmpty(datas)) {
                            break;
                        }
                        j++;
                    }
                } finally {
                    downLatch.countDown();
                }
            });
        }
        try {
            downLatch.await();
            System.out.println("所有子线程执行完毕,total="+total);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "ThreadBatch success";
    }
}

service:

package batch;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class BatchServiceImpl {
    @Autowired
    User userMapper;
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public List<JSONObject> updataSelect(long id,int j){
            //更新
        userMapper.updateBatch(id,j);
        //查询
        List<JSONObject> datas =  userMapper.selectBatch(id,j);
        return datas;
    }

   
}

Mapper:

mark字段是处理次数,默认值0,更新一次值就加1,如果结束后有大于1的值说明该条数据被重复处理了,deal表示当前线程while循环的次数,为了查出当前线程本次循环

更新的数据.

<update id="updateBatch"  >
        update user set threadNum=${id}, mark=mark+1,deal=${j} where mark=0 limit 1000
    </update>
    <select id="selectBatch" resultType="com.alibaba.fastjson.JSONObject">
        select id from user where threadNum=${id} and deal=${j}
    </select>

 

标签:java,重复,org,springframework,util,查询数据库,import,多线程,datas
From: https://www.cnblogs.com/1--2/p/17097290.html

相关文章

  • List<Map<String, Object>> 去出重复
    List<Map<String,Object>>去出重复publicstaticvoidmain(String[]args){String[]array={"name","age"};List<Map<String,Object>>oldList=getTe......
  • Java多线程03——线程安全和线程同步
    1 线程的同步安全1.1线程安全问题设计并发编程的目的是为了使程序获得更高的执行效率,但绝不能出现数据一致性问题。比如多个渠道共同出售电影票,如果没有进行安全控制,就会......
  • Python 高级编程之并发与多线程(三)
    目录一、概述二、并发与并行原理1)并行2)并发3)并发和并行区别三、Python多线程1)进程与线程关系2)Python多线程GIL介绍3)Python创建多线程1、thread2、threading(常用)4)守护线......
  • GROUP BY结合HAVING实现查询重复数据
    GROUPBY字段名,以该字段名已经分类,HAVING可以对分完类的数据使用聚合函数例如SELECT*FROMstudentWHEREstu_nameIN(SELECTstu_nameFROMstudentGROUPBY......
  • spring 重复注解和aop拦截的实现示例
    前言:1:jdk1.8开始支持重复注解@Repeatable实现2:aop拦截需要拦截当前注解和@Repeatable指向的包装注解才可以完全拦截到,因为:1.当在在方法上只有一个注解时,aop拦截......
  • 详解Spring AOP自定义可重复注解没有生效问题
    目录1.问题背景2.不啰嗦,上代码3.问题排查3.1是不是切点写得有问题,于是换成如下形式:3.2是不是使用的地方不是代理对象4.问题原因 1.问题背景工作中遇......
  • java8 list取出重复值
    @Testpublicvoidtest10(){//根据device_code去重,取出重复值List<Employee>emps=newArrayList<>();List<String>dupList=emps.stream().colle......
  • sql语句重复数据的查找和删除
    一、查找重复记录1.查找全部重复记录单字段:Select*From表Where重复字段In(Select重复字段From表GroupBy重复字段HavingCount(*)>1)多字段:selectidfrom......
  • 面试八--多线程(一)线程创建的四种方法
    1进程和线程的概念进程是程序的运行实例,线程是进程中独立执行的最小单位2线程的创建、启动与应用在Java平台中创建一个线程就是创建一个Thread类的实例。线程的任务处理可......
  • 《剑指Offer》-3-数组中重复的数字
    我觉得这道题完全可以出难一点,可以返回有哪些数字重复了,分别重复了几次经评论区提醒,或许考察的是与面试官的沟通能力,对不同解法的衡量取舍:比如时间优先可以用set轻松解......