初始化线程池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("任务已经完成");
});