List<List<Log>> partionList = new ArrayList<>();
if (param.size() >= 1000) {
partionList = Lists.partition(param, 1000);
} else {
partionList.add(param);
}
CountDownLatch countDownLatch = new CountDownLatch(partionList.size());
ExecutorService executor = new ThreadPoolExecutor(
6,
6,
1,
TimeUnit.MICROSECONDS,
new ArrayBlockingQueue<>(6),
new ThreadFactoryBuilder().setNameFormat("create-log-%d").build()
);
for (List<Log> part: partionList) {
executor.submit(() -> {
try {
shipPlanLogDao.insertBatchSomeColumn(part);
} catch (Exception e) {
LogUtil.error("create shipPlanLogDao insertBatchSomeColumn error", e);
}
countDownLatch.countDown();
});
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
LogUtil.error("create log insertBatchSomeColumn error", e);
throw new RuntimeException(e);
}finally {
executor.shutdown();
}
多线程--线程池实现
最新推荐文章于 2024-07-10 21:37:49 发布