How to wait for a number of threads to complete?

You put all threads in an array, start them all, and then have a loop

for(i = 0; i < threads.length; i++)
  threads[i].join();

Each join will block until the respective thread has completed. Threads may complete in a different order than you joining them, but that’s not a problem: when the loop exits, all threads are completed.

Leave a Comment