Package org.apache.druid.concurrent
Class ConcurrentAwaitableCounter
- java.lang.Object
-
- org.apache.druid.concurrent.ConcurrentAwaitableCounter
-
public final class ConcurrentAwaitableCounter extends Object
This synchronization object allows toincrement()
a counter without blocking, potentially from multiple threads (although in some use cases there is just one incrementer thread), and block in other thread(s), awaiting when the count reaches the provided value: seeawaitCount(long)
, or the specified number of events since the call: seeawaitNextIncrements(long)
. This counter wraps aroundLong.MAX_VALUE
and starts from 0 again, so "next" count should be generally obtained by callingnextCount(currentCount)
rather thancurrentCount + 1
. Memory consistency effects: actions in threads prior to callingincrement()
while the count was less than the awaited value happen-before actions following count awaiting methods such asawaitCount(long)
.
-
-
Constructor Summary
Constructors Constructor Description ConcurrentAwaitableCounter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
awaitCount(long totalCount)
Await until theincrement()
is called on this counter object the specified number of times from the creation of this counter object.void
awaitCount(long totalCount, long timeout, TimeUnit unit)
Await until theincrement()
is called on this counter object the specified number of times from the creation of this counter object, for not longer than the specified period of time.boolean
awaitFirstIncrement(long timeout, TimeUnit unit)
The difference between this method andawaitCount(long, long, TimeUnit)
with argument 1 is thatawaitFirstIncrement()
returns boolean designating whether the count was await (while waiting for no longer than for the specified period of time), whileawaitCount()
throwsTimeoutException
if the count was not awaited.void
awaitNextIncrements(long nextIncrements)
Somewhat loosely defined wait for "next N increments", because the starting point is not defined from the Java Memory Model perspective.void
increment()
Increment the count.static long
nextCount(long prevCount)
This method should be called to obtain the next total increment count to be passed toawaitCount(long)
methods, instead of just adding 1 to the previous count, because the count must wrap aroundLong.MAX_VALUE
and start from 0 again.
-
-
-
Method Detail
-
nextCount
public static long nextCount(long prevCount)
This method should be called to obtain the next total increment count to be passed toawaitCount(long)
methods, instead of just adding 1 to the previous count, because the count must wrap aroundLong.MAX_VALUE
and start from 0 again.
-
increment
public void increment()
Increment the count. This method could be safely called from concurrent threads.
-
awaitCount
public void awaitCount(long totalCount) throws InterruptedException
Await until theincrement()
is called on this counter object the specified number of times from the creation of this counter object.- Throws:
InterruptedException
-
awaitCount
public void awaitCount(long totalCount, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException
Await until theincrement()
is called on this counter object the specified number of times from the creation of this counter object, for not longer than the specified period of time. If by this time the target increment count is not reached,TimeoutException
is thrown.- Throws:
InterruptedException
TimeoutException
-
awaitNextIncrements
public void awaitNextIncrements(long nextIncrements) throws InterruptedException
Somewhat loosely defined wait for "next N increments", because the starting point is not defined from the Java Memory Model perspective.- Throws:
InterruptedException
-
awaitFirstIncrement
public boolean awaitFirstIncrement(long timeout, TimeUnit unit) throws InterruptedException
The difference between this method andawaitCount(long, long, TimeUnit)
with argument 1 is thatawaitFirstIncrement()
returns boolean designating whether the count was await (while waiting for no longer than for the specified period of time), whileawaitCount()
throwsTimeoutException
if the count was not awaited.- Throws:
InterruptedException
-
-