Class ReusableCountLatch
A ReusableCountLatch is initialized with a given count. The waitTillZero() methods block until the current count reaches zero due to invocations of the
decrement() method, after which all waiting threads are released. If zero has been reached
any subsequent invocations of waitTillZero() return immediately. The count can be
increased calling the increment() method and any subsequent thread calling the waitTillZero() method will be blocked again until another zero is reached.
ReusableCountLatch provides more versatility than CountDownLatch as the count doesn't have to be known upfront
and you can reuse this class as many times as you want to. It is also better than a Phaser whose count is limited to 65_535. ReusableCountLatch
instead can count up to 2_147_483_647 (2^31-1).
Great use case for ReusableCountLatch is when you wait for tasks on other threads to
finish, but these tasks could trigger more tasks and it is not known upfront how many will be
triggered in total.
- Since:
- 07/10/16 00:10 AM
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs aReusableCountLatchwith initial count set to 0.ReusableCountLatch(int initialCount) Constructs aReusableCountLatchinitialized with the given count. -
Method Summary
Modifier and TypeMethodDescriptionvoidDecrements the count of the latch, releasing all waiting threads if the count reaches zero.intgetCount()Returns the current count.voidIncrements the count of the latch, which will make it possible to block all threads waiting till count reaches zero.voidCauses the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.booleanwaitTillZero(long timeout, @NotNull TimeUnit unit) Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted, or the specified waiting time elapses.
-
Constructor Details
-
ReusableCountLatch
public ReusableCountLatch(int initialCount) Constructs aReusableCountLatchinitialized with the given count.- Parameters:
initialCount- the number of timesdecrement()must be invoked before threads can pass throughwaitTillZero(). For each additional call of theincrement()method one moredecrement()must be called.- Throws:
IllegalArgumentException- ifinitialCountis negative
-
ReusableCountLatch
public ReusableCountLatch()Constructs aReusableCountLatchwith initial count set to 0.
-
-
Method Details
-
getCount
public int getCount()Returns the current count.- Returns:
- the current count
-
decrement
public void decrement()Decrements the count of the latch, releasing all waiting threads if the count reaches zero.If the current count is greater than zero then it is decremented. If the new count is zero then all waiting threads are re-enabled for thread scheduling purposes.
If the current count equals zero then nothing happens.
-
increment
public void increment()Increments the count of the latch, which will make it possible to block all threads waiting till count reaches zero. -
waitTillZero
Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.If the current count is zero then this method returns immediately.
If the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happen:
- The count reaches zero due to invocations of the
decrement()method; or - Some other thread interrupts the current thread.
If the current thread:
- has its interrupted status set on entry to this method; or
- is interrupted while waiting,
InterruptedExceptionis thrown and the current thread's interrupted status is cleared.- Throws:
InterruptedException- if the current thread is interrupted while waiting
- The count reaches zero due to invocations of the
-
waitTillZero
public boolean waitTillZero(long timeout, @NotNull @NotNull TimeUnit unit) throws InterruptedException Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted, or the specified waiting time elapses.If the current count is zero then this method returns immediately with the value
true.If the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happen:
- The count reaches zero due to invocations of the
decrement()method; or - Some other thread interrupts the current thread; or
- The specified waiting time elapses.
If the count reaches zero then the method returns with the value
true.If the current thread:
- has its interrupted status set on entry to this method; or
- is interrupted while waiting,
InterruptedExceptionis thrown and the current thread's interrupted status is cleared.If the specified waiting time elapses then the value
falseis returned. If the time is less than or equal to zero, the method will not wait at all.- Parameters:
timeout- the maximum time to waitunit- the time unit of thetimeoutargument- Returns:
trueif the count reached zero andfalseif the waiting time elapsed before the count reached zero- Throws:
InterruptedException- if the current thread is interrupted while waiting
- The count reaches zero due to invocations of the
-