Before answering your question, I strongly encourage you to look into ExecutorServices
such as for instance the ThreadPoolExecutor
.
Now to answer your question:
If you want to wait for the previous thread to finish, before you start the next, you add thread.join()
in between:
for(int i = 0; i < 10; i++) {
thread = new Thread(this);
thread.start();
thread.join(); // Wait for it to finish.
}
If you want to kick off 10 threads, let them do their work, and then continue, you join
on them after the loop:
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();