ExecuteService--ThreadPoolExecutor线程池搭配CountDownLatch优化循环请求外部接口效率

初始化线程池ExecuteService

构造线程池的方法有多种,可根据使用场景自由选择。

//固定了线程池中的线程。
ExecutorService threadPool = Executors.newFixedThreadPool(5);
//缓存型池子,动态的增加减少线程
ExecutorService threadPool = Executors.newCachedThreadPool();
//单一线程池
ExecutorService threadPool = Executors.newSingleThreadExecutor();
//调度型线程池
ExecutorService threadPool = Executors.newScheduledThreadPool(3);

本文采用如下方法,构建ExecuteService线程池,线程数根据cpu核心数进行设置。

//线程自增编号
private AtomicInteger atomicInteger = new AtomicInteger();

//最大及核心线程数,cpu核心数2倍
private Integer threadCount = 16;

//线程池构造方法
private ExecutorService executorService = 
new ThreadPoolExecutor(
    threadCount,
    threadCount,
    300L, 
    TimeUnit.SECONDS,
    new LinkedBlockingQueue<>(),r -> {
        int index = atomicInteger.incrementAndGet();
        log.info("create no " + index + " thread");
        return new Thread(r, "Thread-" + index);
    });

分配任务到线程

使用CountDownLatch等待任务线程执行完毕后,执行日志录入等任务的线程。

//分多线程发送
List<List<Map<String, String>>> partition = Lists.partition(mapList, threadCount - 1);
//同步倒记锁
CountDownLatch countDownLatch = new CountDownLatch(partition.size());
partition.forEach(current -> executorService.execute(() -> {
    current.forEach(entry -> {
        //具体待处理的任务
    });
    //计数器减一
    countDownLatch.countDown();
}));

//等待任务线程执行完毕后,执行此线程
executorService.execute(() -> {
    log.info("等待任务完成");
    try {
    //计数器减为0时,此线程执行
    countDownLatch.await();
    } catch (InterruptedException e) {
       e.printStackTrace();
    log.error("同步等待任务异常");
    }
    log.info("任务已经完成");
});

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值