public class Test implements Runnable{

@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(""+Thread.currentThread().getId());

}
}

 

public class Main {

public static void main(String[] args) {

ExecutorService executorService=Executors.newFixedThreadPool(10);

for (int i = 0; i < 10; i++) {

Test test=new Test();
executorService.execute(test);
}

executorService.shutdown();//关闭线程池
//判断是否所有的线程已经运行完
while (!executorService.isTerminated()) {
System.out.println(executorService.isTerminated());
}
System.out.println(executorService.isTerminated());
System.out.println("All is finished!");
// System.exit(0);//退出
}

}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-13
  • 2022-12-23
  • 2021-11-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-10
  • 2022-12-23
  • 2021-06-14
  • 2021-11-11
  • 2022-12-23
相关资源
相似解决方案