java ThreadPool判断是否所有任务都完成的方法

来源:互联网 发布:男士香水知乎 编辑:程序博客网 时间:2024/05/29 10:45
@Testpublic void test() {ExecutorService threadPool = Executors.newCachedThreadPool();Future<String> future1 = threadPool.submit(new Callable<String>() {@Overridepublic String call() throws Exception {// TODO Auto-generated method stubfor (int i = 0; i < 100000; i++);return null;}});Future<String> future2 = threadPool.submit(new Callable<String>() {@Overridepublic String call() throws Exception {// TODO Auto-generated method stubfor (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");}

0 0
原创粉丝点击