@Test
public void test() {
ExecutorService threadPool = Executors.newCachedThreadPool();
Future<String> future1 = threadPool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
for (int i = 0; i < 100000; i++)
;
return null;
}
});
Future<String> future2 = threadPool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
for (int i = 0; i < 100000; i++)
;
return null;
}
});
// 停止加入新线程
threadPool.shutdown();
// 当任务1和任务2都完成之后,才算完成.
while (!(future1.isDone() && future2.isDone())) {
System.out.println("f1:" + future1.isDone());
System.out.println("f2:" + future2.isDone());
}
System.out.println("work accomplished");
}
java ThreadPool判断是否所有任务都完成的方法
最新推荐文章于 2025-01-16 15:02:04 发布