ExecutorService es = Executors.newFixedThreadPool(THREADPOOLSIZE+1);
while(true){
long startTime2 = System.currentTimeMillis();
numIids = getIds(batchId, LIMITSIZE);
if (numIids == null || numIids.isEmpty()) {
break;
}
int i = 0;
int batchSize = numIids.size() / THREADPOOLSIZE;
if (numIids.size() > THREADPOOLSIZE) {
for (i = 0; i < THREADPOOLSIZE; i++) {
List<Long> subList = numIids.subList(i * batchSize, ((i + 1) * batchSize));
es.submit(() -> {
try {
compute(batchId, subList);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
if (i * batchSize < numIids.size()) {
List<Long> subList = numIids.subList(i * batchSize, numIids.size());
es.submit(() -> {
try {
compute(batchId, subList);
} catch (Exception e) {
e.printStackTrace();
}
});
}
我想在这里实现等待上面所有子线程完成所有任务,然后向下执行(不关闭线程池中的线程,让这些线程重复使用)
}
es.shutdown();
awaitTerminationQuietly(es);
}
You can use the java.util.concurrent.CountDownLatch class
Look at the invokeAll method