线程池子线程先执行再执行主线程

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);//退出
}

}

原文地址:https://www.cnblogs.com/liuying1995/p/6780368.html