[英]Executorservice Check All the task are done
我想在提交新的一批任务之前检查是否所有的执行程序服务任务都完成了,这里是我的代码:
ExecutorService executorService = Executors.newFixedThreadPool(concurrency);
for(int i=0;i<batch;i++){
for(int j=0;j<concurrency;j++) {
executorService.submit(new DOTASK(0,loopTimes));
}
while(executorService != done){
// wait
}
System.out.println("Batch "+i+"Complete")
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.DAYS);
CountDownLatch是您的一个选择
ExecutorService executorService = Executors.newFixedThreadPool(concurrency);
for(int i=0;i<batch;i++){
CountLatchDown latch = new CountDownLatch(concurrency);
for(int j=0;j<concurrency;j++) {
executorService.submit(new DOTASK(0,loopTimes));
}
//in each DOTASK thread invoke latch.countDown()
latch.await();
System.out.println("Batch "+i+"Complete")
}
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.DAYS);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.