我在mulithreads環境中有問題。我可以創建幾個正確執行的線程,但是這個過程永遠不會結束。我不能等待整理。 我想在所有線程結束但是目前不可能的情況下執行一些操作。這裏我的代碼:我不能等我的線程在java
public static void main(String[] args){
public void run(){
ExecutorService exec = Executors.newFixedThreadPool(10);
try {
for(int i=0;i<100;i++){
final Integer a=i;
try {
exec.submit(new Runnable() {
@Override
public void run() {
System.out.println(a);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
有人知道如何實現我的目標?謝謝!
你在哪裏調用'通知()'? – Kayaman
爲什麼在線程中創建一個新的線程池?你已經有了一個線程來執行。 –
也看看http://stackoverflow.com/questions/1250643/how-to-wait-for-all-threads-to-finish-using-executorservice。 –