9

In the docs for CountDownLatch I see something like:

public void run() {
      try {
        startSignal.await();
        doWork();
        doneSignal.countDown();
      } catch (InterruptedException ex) {} // return;
}

Here startSignal and doneSignal are CountDownLatch objects.

The docs don't mention anything about the class being thread-safe or not.

OO7
  • 2,785
  • 1
  • 21
  • 33
treecoder
  • 43,129
  • 22
  • 67
  • 91
  • 1
    Define *thread-safe* in this case. – OldCurmudgeon May 06 '15 at 11:04
  • Thread safely, in this case would be, for example, to ensure that only one thread calls `countDown` at a time. The signature does not say it is synchronized: `public void countDown()` – treecoder May 06 '15 at 11:10
  • `CountDownLatch` guarantees that `await` will block until `countDown()` is called exactly the specified number of times no matter how many threads might call it at the same time. – Misha May 06 '15 at 11:40

2 Answers2

7

As it is designed to be used by multiple threads it would be fair to assume that it is thread-safe to most meanings of thread-safe.

There is even a happens-before commitment (from your link):

Memory consistency effects: Until the count reaches zero, actions in a thread prior to calling countDown() happen-before actions following a successful return from a corresponding await() in another thread.

With reference to your specific question What if two threads call countDown at the same time? Wouldn't it just do the count down action only once effectively? No, two countDowns will be actioned every time.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • But shouldn't calling `countDown` itself be synchronized ? What if two threads call `countDown` at the same time? Wouldn't it just do the count down action only once effectively? – treecoder May 06 '15 at 11:09
  • 3
    @treecoder you seem to equate "a method is thread-safe" with "a method is synchronized". That's incorrect. There are several ways to make a method thread-safe without making it synchronized. CountDownLatch internally uses non-blocking thread-safe operations. Everything under java.util.concurrent is designed to be used by multiple threads. – JB Nizet May 06 '15 at 11:51
0

Yes the class or rather the methods you call on a CountDownLatch objects arr thread-safe.

In order to make these operations such as countDown() await() thread-safe, they have not used synchronize block or functions. Rather they have used Compare and Swap strategy. Below is the source codes which proves the same

sync.releaseShared(1);

public final boolean releaseShared(int arg) {
    if (tryReleaseShared(arg)) {
        doReleaseShared();
        return true;
    }
    return false;
}

protected boolean tryReleaseShared(int releases) {
        // Decrement count; signal when transition to zero
        for (;;) {
            int c = getState();
            if (c == 0)
                return false;
            int nextc = c-1;
            if (compareAndSetState(c, nextc))
                return nextc == 0;
        }
    }

The above code is a part of the total implementation, you can check source codes for other methods like await() as well.

Dibyendu
  • 335
  • 2
  • 7