2

What happen if multiple threads will call await() method of CountDownLatch?

We have for example 3 threads Thread1, Thread2, Thread3. Thread1 has running job. Thread2,Thread3 will call await method. What will be a result?

Thread2 will be resumed at the same time as Thread3 or Thread3 will wait for Thread2 ending?

xingbin
  • 27,410
  • 9
  • 53
  • 103
Dawid Macura
  • 193
  • 1
  • 9
  • Re, "...at the same time..." Those words have very little meaning. All you really can say about `countdownlatch.await()` is that it will decrement the value of the `countdownlatch`, and it will not return until the value is less than or equal to zero. There is no specification that says _how soon after_ the value reaches zero the call will return. On a system that has fewer processors than the number of `await()`ing threads, it would be physically impossible for all of the threads to wake up at the "same time." – Solomon Slow Mar 07 '19 at 13:32

1 Answers1

2

Thread2 and Thread3 will get waken up sequencely, but the time difference is really small. And there is no guarantee which of them will get waken up first. Basically, Thread2 will be resumed at the same time as Thread3.

xingbin
  • 27,410
  • 9
  • 53
  • 103