private List<Object> batchSelect(final Integer count) {
//根据查询条件查询数据
List<Object> listDto = new ArrayList<>();
CountDownLatch latch = new CountDownLatch((count % BATCHNUM == 0) ? count / BATCHNUM : count / BATCHNUM + 1);
ExecutorService execThreadPool = Executors.newFixedThreadPool(3, new ThreadUseUtils.NamedThreadFactory("batchSelect"));
for (int line = 0, size = count, num = count / BATCHNUM; line <= num; line++) {
int finalLine = line;
Thread thread = new Thread(() -> {
try {
PageHelper.startPage(finalLine, BATCHNUM);
List<Object> listDtoThread = mapper.selectList();
listDto.addAll(listDtoThread);
} finally {
if (latch != null)
latch.countDown();
}
});
thread.setUncaughtExceptionHandler(new ThreadUseUtils.MyUncaughtExceptionHandler());
ThreadUseUtils.executorService.execute(thread);
}
try {
latch.await();
} catch (InterruptedException e) {
log.error("InterruptedException:latch.await()", e.toString());
}
log.info(listDto.toString());
if (execThreadPool != null && !execThreadPool.isShutdown()) execThreadPool.shutdownNow();
return listDto;
}
Java多线程分页查询数据库并返回
最新推荐文章于 2024-09-30 09:20:12 发布