Take the 2-minute tour ×

I'm writing an application that has 5 threads that get some information from web simultaneously and fill 5 different fields in a buffer class.
I need to validate buffer data and store it in a database when all threads finished their job.
How can I do this (get alerted when all threads finished their work) ?

share|improve this question
Thread.join is a rather low-level very Java idiosynchratic way to solve the issue. Moreover it's problematic because the Thread API is flawed: you cannot know whether the join completed successfuly or not (see Java Concurrency In Practice). Higher level abstraction, like using a CountDownLatch may be preferrable and will look more natural to programmers who aren't "stuck" in the Java-idiosynchratic mindset. Don't argue with me, go argue with Doug Lea ; ) –  Cedric Martin Oct 29 '11 at 13:49
possible duplicate of How to wait for a set of threads to complete? –  ChrisF Nov 25 '11 at 14:30
add comment

9 Answers

up vote 16 down vote accepted

The approach I take is to use an ExecutorService to manage pools of threads.

ExecutorService es = Executors.newCachedThreadPool();
    es.execute(new Runnable() { /*  your task */ });
boolean finshed = es.awaitTermination(1, TimeUnit.MINUTES);
// all tasks have finished or the time has been reached.
share|improve this answer
Be aware that this approach will not work for thread pools as they are not terminated after a set of tasks. –  Leonid Oct 11 '13 at 17:03
@Leonid that is exactly what shutdown() does. –  Peter Lawrey Oct 11 '13 at 21:05
add comment

You can join to the threads. The join blocks until the thread completes.

for (Thread thread : threads) {

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).

share|improve this answer
Do these threads run in parallel or sequentially to each other? –  James Webster Oct 29 '11 at 13:45
@JamesWebster: Parallel. –  4r1y4n Oct 29 '11 at 13:47
The join just makes it sequential to the main thread? –  James Webster Oct 29 '11 at 13:48
@James Webster: The statement t.join(); means that the current thread blocks until the thread t terminates. It does not affect thread t. –  Mark Byers Oct 29 '11 at 13:50
Thanks. =] Studied paralellism at uni, but that was the sole thing I struggled to learn! Thankfully I don't have to use it much now or when I do it's not too complex or there are no shared resources and blocking isn't critical –  James Webster Oct 29 '11 at 14:10
show 1 more comment

Apart from Thread.join() suggested by others, java 5 introduced the executor framework. There you don't work with Thread objects. Instead, you submit your Callable or Runnable objects to an executor. There's a special executor that is meant to execute multiple tasks and return their results out of order. That's the ExecutorCompletionService:

ExecutorCompletionService executor;
for (..) {

Then you can repeatedly call take() until there are no more Future<?> objects to return, which means all of them are completed.

Another thing that may be relevant, depending on your scenario is CyclicBarrier.


share|improve this answer
This is close, but I'd still make a couple of adjustments. executor.submit returns a Future<?>. I would add these futures to a list, and then loop through the list calling get on each future. –  Ray Oct 31 '11 at 12:41
Also, you can instantiate a constructor using Executors, e.g., Executors.newCachedThreadPool (or similar) –  Ray Oct 31 '11 at 12:42
add comment

Another possibility is the CountDownLatch object, which is useful for simple situations : since you know in advance the number of threads, you initialize it with the relevant count, and pass the reference of the object to each thread.
Upon completion of its task, each thread calls CountDownLatch.countDown() which decrements the internal counter. The main thread, after starting all others, should do the CountDownLatch.await() blocking call. It will be released as soon as the internal counter has reached 0.

Pay attention that with this object, an InterruptedException can be thrown as well.

share|improve this answer
add comment

You do

for (Thread t : new Thread[] { th1, th2, th3, th4, th5 })

After this for loop, you can be sure all threads have finished their jobs.

share|improve this answer
add comment

You can use Threadf#join method for this purpose.

share|improve this answer
add comment

Store the Thread-objects into some collection (like a List or a Set), then loop through the collection once the threads are started and call join() on the Threads.

share|improve this answer
add comment

An executor service can be used to manage multiple threads including status and completion. See http://programmingexamples.wikidot.com/executorservice

share|improve this answer
add comment

Although not relevant to OP's problem, if you are interested in synchronization (more precisely, a rendez-vous) with exactly one thread, you may use an Exchanger http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Exchanger.html

In my case, I needed to pause the parent thread until the child thread did something, e.g. completed its initialization. A CountDownLatch also works well.

share|improve this answer
add comment

Your Answer


