In my program, I am creating several threads in the main() method. The last line in the main method is a call to System.out.println(), which I don\'t want to call until all
You have better options if you go for ExecutorService or ThreadPoolExecutor framework.
invokeAll
Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list. Note that a completed task could have terminated either normally or by throwing an exception. The results of this method are undefined if the given collection is modified while this operation is in progress. Type Parameters:
CountDownLatch : Initialize CountDownLatch with counter as number of threads. Use countDown()
and await()
APIs and wait for counter to become zero.
Further references:
How to use invokeAll() to let all thread pool do their task?
How is CountDownLatch used in Java Multithreading?