You can join
to the threads. The join blocks until the thread completes.
for (Thread thread : threads) {
thread.join();
}
Note that join
throws an InterruptedException
. You'll have to decide what to do if that happens (e.g. try to cancel the other threads to prevent unnecessary work being done).