java10个线程批量插入或更新数据

 

public void getTemp() throws InterruptedException {
        //我的业务需求 先查出来所有的住户
        List houseHolds = houseMapper.selectHouses();
        int threadNum = 10;//10个线程
        ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        int perSize = houseHolds.size() / threadNum;
        //剩余
        int surplus = houseHolds.size() - threadNum * perSize;
        for (int i = 0; i < threadNum; i++) {
            
            MyThread thread = new MyThread();
            // 最后一次将剩余一次性执行
            if (i == threadNum - 1) {
                thread.setIdList(houseHolds.subList(i * perSize, (i + 1) * perSize + surplus));
            } else {
                thread.setIdList(houseHolds.subList(i * perSize, (i + 1) * perSize));
            }
            thread.setCountDownLatch(countDownLatch);
            executorService.submit(thread);
        }
        countDownLatch.await();
        executorService.shutdown();
    }

2. 在此处会涉及到 线程注入@Autowired mapper为null的问题 解决方法第二个代码片

public class MyThread extends Thread {

    private List idList;

    private CountDownLatch countDownLatch;

    public void setIdList(List idList) {
        this.idList = idList;
    }

    public void setCountDownLatch(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }
//解决线程获取bean为空,通过spring的上下文去手动获取,在线程启动时得到bean
    TempMapper tempMapper;

    public MyThread(){
        tempMapper = SpringUtil.getBean(TempMapper.class);
    }

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            System.out.println(this.idList);
            for (JSONObject jsonObject : idList) {
                jsonObject.put("getTime",new Date());
                jsonObject.put("aaTemp",25.9);
                jsonObject.put("bbTemp",23.2);
                jsonObject.put("ccTemp",22.02);
            }
            int i = tempMapper.insertOrUpdateBatch(idList);
            System.out.println(i);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (countDownLatch != null) {
                countDownLatch.countDown();
            }
        }
    }
}

3.解决线程获取bean为空,通过spring的上下文去手动获取,在线程启动时得到bean 工具类

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    //通过class获取Bean.
    public static  T getBean(Class clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static  T getBean(String name,Class clazz){
        return getApplicationContext().getBean(name, clazz);
    }

}

4.批量插入或更新sql


    insert into t_temp (aa,bb,cc,dd,ee,ff)
    values
    
      (
       #{item.aa},#{item.bb},#{item.cc},#{item.dd},#{item.ee},#{item.ff}
      )
    
    on duplicate key
    update
    aa=values(aa),bb=values(bb),cc=values(cc),dd=values(dd),ee=values(ee),ff=values(ff)
  

 

你可能感兴趣的:(java)