public final class ConcurrentUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int |
CONCURRENT_EMITTING |
static int |
CONCURRENT_IDLE |
Modifier and Type | Method and Description |
---|---|
static <T,R> long |
drainSingleConsumerCollectionDelayThrow(java.util.Collection<T> source,
java.util.function.Consumer<T> forEach,
java.util.concurrent.atomic.AtomicIntegerFieldUpdater<R> drainActiveUpdater,
R flagOwner)
Drains the passed single-consumer
Collection and ensures that it is empty before returning. |
static <T,R> long |
drainSingleConsumerQueue(java.util.Queue<T> queue,
java.util.function.Consumer<T> forEach,
java.util.concurrent.atomic.AtomicIntegerFieldUpdater<R> drainActiveUpdater,
R flagOwner)
Drains the passed single-consumer
Queue and ensures that it is empty before returning. |
static <T,R> long |
drainSingleConsumerQueueDelayThrow(java.util.Queue<T> queue,
java.util.function.Consumer<T> forEach,
java.util.concurrent.atomic.AtomicIntegerFieldUpdater<R> drainActiveUpdater,
R flagOwner)
Drains the passed single-consumer
Queue and ensures that it is empty before returning. |
static <T> void |
executeExclusive(java.lang.Runnable task,
java.util.concurrent.atomic.AtomicIntegerFieldUpdater<T> exclusionFlag,
T flagOwner)
Executes the passed
Runnable assuring that concurrent invocations of this method does not concurrently
execute the Runnable . |
public static final int CONCURRENT_IDLE
public static final int CONCURRENT_EMITTING
public static <T,R> long drainSingleConsumerQueue(java.util.Queue<T> queue, java.util.function.Consumer<T> forEach, java.util.concurrent.atomic.AtomicIntegerFieldUpdater<R> drainActiveUpdater, R flagOwner)
Queue
and ensures that it is empty before returning.
This accounts for any additions to the Queue
while drain is in progress.
Multiple threads can call this method concurrently but only one thread will actively drain the Queue
.T
- Type of items stored in the Queue
.R
- Type of the object holding the int
referred by AtomicIntegerFieldUpdater
.queue
- Queue
to drain.forEach
- Consumer
for each item that is drained.drainActiveUpdater
- An AtomicIntegerFieldUpdater
for an int
that is used to guard against
concurrent drains.flagOwner
- Holding instance for drainActiveUpdater
.public static <T,R> long drainSingleConsumerQueueDelayThrow(java.util.Queue<T> queue, java.util.function.Consumer<T> forEach, java.util.concurrent.atomic.AtomicIntegerFieldUpdater<R> drainActiveUpdater, R flagOwner)
Queue
and ensures that it is empty before returning.
This accounts for any additions to the Queue
while drain is in progress.
Multiple threads can call this method concurrently but only one thread will actively drain the Queue
.
Any Throwable
thrown by forEach
Consumer
does not terminate draining but throws all
thrown Throwable
s after drain.T
- Type of items stored in the Queue
.R
- Type of the object holding the int
referred by AtomicIntegerFieldUpdater
.queue
- Queue
to drain.forEach
- Consumer
for each item that is drained.drainActiveUpdater
- An AtomicIntegerFieldUpdater
for an int
that is used to guard against
concurrent drains.flagOwner
- Holding instance for drainActiveUpdater
.java.lang.RuntimeException
- All Throwable
thrown by forEach
Consumer
are added as suppressed
causes.public static <T,R> long drainSingleConsumerCollectionDelayThrow(java.util.Collection<T> source, java.util.function.Consumer<T> forEach, java.util.concurrent.atomic.AtomicIntegerFieldUpdater<R> drainActiveUpdater, R flagOwner)
Collection
and ensures that it is empty before returning.
This accounts for any additions to the Collection
while drain is in progress.
Multiple threads can call this method concurrently but only one thread will actively drain the
Collection
. Any Throwable
thrown by forEach
Consumer
does not terminate draining
but throws all thrown Throwable
s after drain.T
- Type of items stored in the Collection
.R
- Type of the object holding the int
referred by AtomicIntegerFieldUpdater
.source
- Collection
to drain.forEach
- Consumer
for each item that is drained.drainActiveUpdater
- An AtomicIntegerFieldUpdater
for an int
that is used to guard against
concurrent drains.flagOwner
- Holding instance for drainActiveUpdater
.java.lang.RuntimeException
- All Throwable
thrown by forEach
Consumer
are added as suppressed
causes.public static <T> void executeExclusive(java.lang.Runnable task, java.util.concurrent.atomic.AtomicIntegerFieldUpdater<T> exclusionFlag, T flagOwner)
Runnable
assuring that concurrent invocations of this method does not concurrently
execute the Runnable
. However, any invocation, while another execution is in progress, will trigger
execution of the Runnable
again after the current execution completes. This process will repeat till no
invocation was received during the execution of the Runnable
or any execution threw an exception.T
- Type of the owner of the flags.task
- Runnable
to execute. Any unchecked exception thrown by this task is thrown from this method.exclusionFlag
- AtomicIntegerFieldUpdater
to update a flag that guarantees exclusive execution of
the task. This flag is assumed to be 0
when this method is invoked and must not be modified elsewhere.flagOwner
- Owning instance of the