Use a CountdownLatch
. The Javadoc for that class tells how to have
- The main thread creates the latch with the number of threads.
- The main thread starts all the working threads.
- Each thread has a reference to the latch.
- It counts the latch down when it finishes its work.
- The main thread waits for the latch to count down to 0.
- The main thread does the printing job.