36

When waiting for other threads to finish, we can use either join or CountdownLatch. What are the pros and cons of using either of those two mechanisms?

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • `CountDownLatch` provides coordination between the threads themselves. – obataku Sep 03 '12 at 22:39
  • @veer - 1) it depends on how you use it. 2) it is clear that the OP is not using it for that purpose - *"When waiting other threads to finish ..."* – Stephen C Sep 03 '12 at 23:04

5 Answers5

34

You can only use Thread.join if you're handling the threads yourself. Most people choose not to deal with the minutia of thread handling directly, and instead use an ExecutorService to handle it for them. ExecutorServices do not directly reveal how they are executing tasks, so you would have to use a CountDownLatch: (Assuming you don't want to just shutdown the whole service, that is.)

ExecutorService service = Executors.newFixedThreadPool(5);
final CountDownLatch latch = new CountDownLatch(5);

for(int x = 0; x < 5; x++) {
    service.submit(new Runnable() {
        public void run() {
            // do something
            latch.countDown();
        }
    });
}

latch.await();
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
21

Another difference is after join(), thread can be unblocked only when joined thread has finished its execution while in CountDownLatch a thread can decrease the count anytime either on completion of thread or in between based on any condition.
This way we can get better control over unblocking of the thread instead of solely depending on the completion of joined thread.

Rajesh Gupta
  • 233
  • 2
  • 5
15

join() is waiting for another thread to finish while CountDownLatch is designed for another purpose. If using CountDownLatch, You don't have to have reference of threads for which you are waiting as we have to do using join(). Let's assume you want to start a game when at least 2 players should be available. You can use countdownlatch in this case. But you can't achieve this using join easily because you don't have another thread(player in this case) on which you can write join().

Aman Goyal
  • 383
  • 2
  • 9
10

A CountdownLatch is task-oriented - it's thread-agnostic. A whole pile of unrelated sets of tasks can be submitted to a threadPool and a CountdownLatch will ensure that each set notifies the originator of completion. Join() is an annoying abberation that links tasks to threads and, simply put, should never have entered the language in the first place. Sadly, a large, steaming pile of thread-tutorials mention Join() on the first page, thereby introducing threads to newbies as a deadlock-generator and generating thread-funk :(

Martin James
  • 24,453
  • 3
  • 36
  • 60
4

CountdownLatchallows you to change the implementation of Item to maybe submit to an Executor service instead of using Threads directly.

The CountDownLatch class allows us to coordinate the starting and stopping of threads. Typical uses are as follows:

  1. We can make several threads start at the same time;
  2. We can wait for several threads to finish (whereas, for example, the Thread.join() method only lets you wait for a single thread).

You can have a look at this -> http://javahowto.blogspot.com/2011/08/when-to-join-threads-with.html

And this -> A CountDownLatch's latch.await() method vs Thread.join()

Community
  • 1
  • 1
Anuj Kulkarni
  • 2,261
  • 5
  • 32
  • 42