線程池子線程先執行再執行主線程


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

}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com