Class ReusableCountLatch
- java.lang.Object
-
- io.sentry.transport.ReusableCountLatch
-
public final class ReusableCountLatch extends java.lang.ObjectClass originally copied from ReusableCountLatch.java.A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
A
ReusableCountLatchis initialized with a given count. ThewaitTillZero()methods block until the current count reaches zero due to invocations of thedecrement()method, after which all waiting threads are released. If zero has been reached any subsequent invocations ofwaitTillZero()return immediately. The coun cen be increased calling theincrement()method and any subsequent thread calling thewaitTillZero()method will be block again until another zero is reached.ReusableCountLatchprovides more versatility thanCountDownLatchas 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 aPhaserwhose count is limited to 65_535.ReusableCountLatchinstead can count up to 2_147_483_647 (2^31-1).Great use case for
ReusableCountLatchis when you wait for tasks on other threads to finish, but these tasks could trigger more tasks and it is not know upfront how many will be triggered in total.- Since:
- 07/10/16 00:10 AM
-
-
Constructor Summary
Constructors Constructor Description ReusableCountLatch()Constructs aReusableCountLatchwith initial count set to 0.ReusableCountLatch(int initialCount)Constructs aReusableCountLatchinitialized with the given count.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddecrement()Decrements the count of the latch, releasing all waiting threads if the count reaches zero.intgetCount()Returns the current count.voidincrement()Increments the count of the latch, which will make it possible to block all threads waiting till count reaches zero.voidwaitTillZero()Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.booleanwaitTillZero(long timeout, @NotNull java.util.concurrent.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 Detail
-
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:
java.lang.IllegalArgumentException- ifinitialCountis negative
-
ReusableCountLatch
public ReusableCountLatch()
Constructs aReusableCountLatchwith initial count set to 0.
-
-
Method Detail
-
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
public void waitTillZero() throws java.lang.InterruptedExceptionCauses 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:
java.lang.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 java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionCauses 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:
java.lang.InterruptedException- if the current thread is interrupted while waiting
- The count reaches zero due to invocations of the
-
-