public final class ConcurrentAwaitableCounter extends Object
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 and Description |
|---|
ConcurrentAwaitableCounter() |
| Modifier and Type | Method and Description |
|---|---|
void |
awaitCount(long totalCount)
Await until the
increment() 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 the
increment() 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 and
awaitCount(long, long, TimeUnit) with argument 1 is that awaitFirstIncrement() returns boolean designating whether the count was await (while waiting for no longer than
for the specified period of time), while awaitCount() throws TimeoutException 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 to
awaitCount(long) methods,
instead of just adding 1 to the previous count, because the count must wrap around Long.MAX_VALUE and start
from 0 again. |
public static long nextCount(long prevCount)
awaitCount(long) methods,
instead of just adding 1 to the previous count, because the count must wrap around Long.MAX_VALUE and start
from 0 again.public void increment()
public void awaitCount(long totalCount)
throws InterruptedException
increment() is called on this counter object the specified number of times from the creation
of this counter object.InterruptedExceptionpublic void awaitCount(long totalCount,
long timeout,
TimeUnit unit)
throws InterruptedException,
TimeoutException
increment() 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.InterruptedExceptionTimeoutExceptionpublic void awaitNextIncrements(long nextIncrements)
throws InterruptedException
InterruptedExceptionpublic boolean awaitFirstIncrement(long timeout,
TimeUnit unit)
throws InterruptedException
awaitCount(long, long, TimeUnit) with argument 1 is that awaitFirstIncrement() returns boolean designating whether the count was await (while waiting for no longer than
for the specified period of time), while awaitCount() throws TimeoutException if the count was not
awaited.InterruptedExceptionCopyright © 2011–2023 The Apache Software Foundation. All rights reserved.