在回答您的问题之前,我强烈建议您查看
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();