JAVA多线程+事务进行批量新增

线程池的配置:

@Configuration
@EnableAsync
@Slf4j
public class ThreadPoolConfig {

    @Bean("threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor buildThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        threadPoolTaskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
        // 设置最大线程数
        threadPoolTaskExecutor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2);
        // 设置队列容量
        threadPoolTaskExecutor.setQueueCapacity(0);
        // 设置线程活跃时间(秒)
        threadPoolTaskExecutor.setKeepAliveSeconds(20);
        // 设置默认线程名称
        threadPoolTaskExecutor.setThreadNamePrefix("ThreadPool-");
        // 设置拒绝策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        // 等待所有任务结束后再关闭线程池
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }
}

代码实现:

 //根据数据库不同,现定义每次最大插入量2000通用
    private static final int INSERT_LIMIT_NUMBER = 2000;
    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
**
     * 异步新增
     *
     * @param getAllShiftsResponseList
     * @param monthFullDay
     */
    private void threadInsert(List<GetAllShiftsVo> getAllShiftsResponseList, List<String> monthFullDay) {
        int eachInsertSize = INSERT_LIMIT_NUMBER;
        int maxInsertPoolSize = threadPoolTaskExecutor.getMaxPoolSize();
        if (getAllShiftsResponseList.size() > eachInsertSize * maxInsertPoolSize) {
            eachInsertSize = BigDecimal.valueOf(getAllShiftsResponseList.size())
                    .divide(BigDecimal.valueOf(maxInsertPoolSize), BigDecimal.ROUND_UP).intValue();
        }
        List<List<GetAllShiftsVo>> partition = Lists.partition(getAllShiftsResponseList, eachInsertSize);
        int threadNum = partition.size();
        // 是否存在异常
        AtomicReference<Boolean> rollbackFlag = new AtomicReference<>(false);
        CountDownLatch transactionLatch = new CountDownLatch(threadNum);
        partition.forEach(item -> executeInsert(item, rollbackFlag, transactionLatch));
        try {
            if (!transactionLatch.await(200L, TimeUnit.SECONDS)) {
                log.info("执行时间过长!!");
                rollbackFlag.set(true);
            }
        } catch (Exception e) {
            log.info("批量新增每月排班失败!");
            throw new BusinessException(Emsg.BATCH_SHIFT_INSERT_ERROR);
        }
    }
private void executeInsert(List<GetAllShiftsVo> list, AtomicReference<Boolean> rollbackFlag,
                               CountDownLatch transactionLatch) {
        threadPoolTaskExecutor.execute(() -> {
            log.info(Thread.currentThread().getName() + "线程启动");
            if (rollbackFlag.get()) {
                return;
            }
            DefaultTransactionDefinition def = new DefaultTransactionDefinition();
            //事物隔离级别,开启新事务,这样会比较安全些。
            def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            //获得事务状态
            TransactionStatus status = transactionManager.getTransaction(def);
            try {
                log.info("开始插入");
                if (list.size() <= INSERT_LIMIT_NUMBER) {
                    batchInsertAllShiftList(list);
                } else {
                    List<List<GetAllShiftsVo>> listList = Lists.partition(list, INSERT_LIMIT_NUMBER);
                    listList.forEach(item -> batchInsertAllShiftList(item));
                }
                log.info(Thread.currentThread().getName() + "线程等待");
            } catch (Exception e) {
                log.error("批量插入异常", e);
                rollbackFlag.set(true);
                transactionManager.rollback(status);
            }
            // 事务结束,计数阀计数减1
            transactionLatch.countDown();
            try {
                transactionLatch.await();
                if (rollbackFlag.get()) {
                    //事务回滚
                    transactionManager.rollback(status);
                } else {
                    //事务提交
                    transactionManager.commit(status);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.info(Thread.currentThread().getName() + "线程结束");
        });
    }

大致实现逻辑就是这样

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值