如何判断java多线程是否全部执行完了,并计

2024-10-22 17:01:49
推荐回答(1个)
回答1:

思路: 把所有的线程都放到线程池里面
步骤:
1: 创建线程池 ExecutorService cachedThreadPool
2: 开启一个线程 cachedThreadPool.execute(new Runnable() {//做任务})
3.: 判断线程池里面是否执行完, cachedThreadPool.isTerminated()
while (true) {
if (cachedThreadPool.isTerminated()) { // 计算耗时
long time = System.currentTimeMillis() - start;
System.out.println("["+Utils.getNowTime()+"]:" + "程序结束了,总耗时:" + time + " ms(毫秒)!\n");
break;
}
}