views:

2469

answers:

4

Is there an advantage to using java.util.concurrent.CountdownLatch instead of java.util.concurrent.Semaphore? As far as I can tell the following fragments are almost equivalent:

1:

final Semaphore sem = new Semaphore(0);
for (int i = 0; i < num_threads; ++ i)
{
  Thread t = new Thread() {
    public void run()
    {
      try
      {
        doStuff();
      }
      finally
      {
        sem.release();
      }
    }
  };
  t.start();
}

sem.acquire(num_threads);

2:

final CountDownLatch latch = new CountDownLatch(num_threads);
for (int i = 0; i < num_threads; ++ i)
{
  Thread t = new Thread() {
    public void run()
    {
      try
      {
        doStuff();
      }
      finally
      {
        latch.countDown();
      }
    }
  };
  t.start();
}

latch.await();

Except that in case #2 the latch cannot be reused and more importantly you need to know in advance how many threads will be created (or wait until they are all started before creating the latch.)

So in what situation might the latch be preferable?

A: 

CountdownLatch makes threads wait on the await() method, until such a time as the count has reached zero. So maybe you want all your threads to wait until 3 invocations of something, then all the threads can go. A Latch generally can not be reset.

A Semaphore allows threads to retrieve permits, which prevents too many threads from executing at once, blocking if it cannot get the permit(s) it requires to proceed. Permits can be returned to a Semaphore allowing the other waiting threads to proceed.

Spencer K
A: 

Looking at the freely available source, there is no magic in the implementation of the two classes, so there performance should be much the same. Choose the one that makes your intent more obvious.

Tom Hawtin - tackline
+7  A: 

CountDown latch is intended to be used for the exact opposite of your example. Generally, you would have many thins blocking on "await()" that would all start simultaneously when the countown reached zero.

final CountDownLatch countdown = new CountDownLatch(1);
for (int i = 0; i < 10; ++ i){
   Thread racecar = new Thread() {    
      public void run()    {
         countdown.await(); //all threads waiting
         System.out.println("Vroom!");
      }
   };
   racecar.start();
}
System.out.println("Go");
countdown.countDown();   //all threads start now!

You could also use this as an MPI-style "barrier" that causes all threads to wait for other threads to catch up to a certain point before proceeding.

final CountDownLatch countdown = new CountDownLatch(num_thread);
for (int i = 0; i < num_thread; ++ i){
   Thread t= new Thread() {    
      public void run()    {
         doSomething();
         countdown.countDown();
         System.out.printf("Waiting on %d other threads.",countdown.getCount());
         countdown.await();     //waits until everyone reaches this point
         finish();
      }
   };
   t.start();
}
James Schek
Thanks. So my two examples would not be equivalent if multiple threads could wait on the latch... unless sem.acquire(num_threads); is followed by sem.release(num_threads);? I think that would make them equivalent again.
finnw
In a sense, yes, as long as every thread called acquire followed by release. Strictly speaking, no. With a latch, all threads are eligible to start simultaneously. With the semaphore, they become eligible one after another (which could result in different thread scheduling).
James Schek
+2  A: 

CountdownLatch is used to start a series of threads and then wait until all of them are complete (or until they call countDown() a given number of times.

Semaphore is used to control the number of concurrent threads that are using a resource. That resource can be something like a file, or could be the cpu by limiting the number of threads executing. The count on a Semaphore can go up and down as different threads call acquire() and release().

In your example, you're essentially using Semaphore as a sort of Count*UP*Latch. Given that your intent is to wait on all threads finishing, using the CountdownLatch makes your intention clearer.

mtruesdell