public final class RunnableErrorForwarder
extends java.lang.Object
Error
or RuntimeException
from a Runnable
executed in
a worker thread back to the parent thread. The simplified usage pattern looks like this:
RunnableErrorForwarder errorForwarder = new RunnableErrorForwarder(); for ( Runnable task : tasks ) { executor.execute( errorForwarder.wrap( task ) ); } errorForwarder.await();
Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.atomic.AtomicInteger |
counter |
private java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> |
error |
private java.lang.Thread |
thread |
Constructor and Description |
---|
RunnableErrorForwarder()
Creates a new error forwarder for worker threads spawned by the current thread.
|
Modifier and Type | Method and Description |
---|---|
void |
await()
Causes the current thread to wait until all previously
wrapped runnables have terminated
and potentially re-throws an uncaught RuntimeException or Error from any of the runnables. |
private void |
awaitTerminationOfAllRunnables() |
java.lang.Runnable |
wrap(java.lang.Runnable runnable)
Wraps the specified runnable into an equivalent runnable that will allow forwarding of uncaught errors.
|
private final java.lang.Thread thread
private final java.util.concurrent.atomic.AtomicInteger counter
private final java.util.concurrent.atomic.AtomicReference<java.lang.Throwable> error
public RunnableErrorForwarder()
public java.lang.Runnable wrap(java.lang.Runnable runnable)
runnable
- The runnable from which to forward errors, must not be null
.null
.public void await()
wrapped
runnables have terminated
and potentially re-throws an uncaught RuntimeException
or Error
from any of the runnables. In
case multiple runnables encountered uncaught errors, one error is arbitrarily selected. Note: This
method must be called from the same thread that created this error forwarder instance.private void awaitTerminationOfAllRunnables()