Package org.apache.druid.concurrent
Class ConcurrentAwaitableCounter
java.lang.Object
org.apache.druid.concurrent.ConcurrentAwaitableCounter
This synchronization object allows to
increment() 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: see awaitCount(long), or the specified number of events since the
call: see awaitNextIncrements(long).
This counter wraps around Long.MAX_VALUE and starts from 0 again, so "next" count should be generally
obtained by calling nextCount(currentCount) rather than currentCount + 1.
Memory consistency effects: actions in threads prior to calling increment() while the count was less than the
awaited value happen-before actions following count awaiting methods such as awaitCount(long).-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidawaitCount(long totalCount) Await until theincrement()is called on this counter object the specified number of times from the creation of this counter object.voidawaitCount(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.booleanawaitFirstIncrement(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()throwsTimeoutExceptionif the count was not awaited.voidawaitNextIncrements(long nextIncrements) Somewhat loosely defined wait for "next N increments", because the starting point is not defined from the Java Memory Model perspective.voidIncrement the count.static longnextCount(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_VALUEand start from 0 again.
-
Constructor Details
-
ConcurrentAwaitableCounter
public ConcurrentAwaitableCounter()
-
-
Method Details
-
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_VALUEand start from 0 again. -
increment
public void increment()Increment the count. This method could be safely called from concurrent threads. -
awaitCount
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,TimeoutExceptionis thrown.- Throws:
InterruptedExceptionTimeoutException
-
awaitNextIncrements
Somewhat loosely defined wait for "next N increments", because the starting point is not defined from the Java Memory Model perspective.- Throws:
InterruptedException
-
awaitFirstIncrement
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()throwsTimeoutExceptionif the count was not awaited.- Throws:
InterruptedException
-