在回答您的問題之前,我強烈建議您查看
ExecutorServices,例如
ThreadPoolExecutor。
現在回答你的問題:
如果要等待上一個線程完成,在開始下一步之前,您可以在之間添加thread.join():
for(int i = 0; i < 10; i++) {
thread = new Thread(this);
thread.start();
thread.join(); // Wait for it to finish.
}
如果你想啟動10個線程,讓他們做他們的工作,然后繼續,你在循環后加入他們:
Thread[] threads = new Thread[10];
for(int i = 0; i < threads.length; i++) {
threads[i] = new Thread(this);
threads[i].start();
}
// Wait for all of the threads to finish.
for (Thread thread : threads)
thread.join();