@Internal public class ExceptionProxy extends Object
The spawned thread would set the exception via reportError(Throwable)
. The parent
would check (at certain points) for exceptions via checkAndThrowException()
. Optionally,
the parent can pass itself in the constructor to be interrupted as soon as an exception occurs.
final ExceptionProxy errorProxy = new ExceptionProxy(Thread.currentThread());
Thread subThread = new Thread() {
public void run() {
try {
doSomething();
} catch (Throwable t) {
errorProxy.reportError(
} finally {
doSomeCleanup();
}
}
};
subThread.start();
doSomethingElse();
errorProxy.checkAndThrowException();
doSomethingMore();
errorProxy.checkAndThrowException();
try {
subThread.join();
} catch (InterruptedException e) {
errorProxy.checkAndThrowException();
// restore interrupted status, if not caused by an exception
Thread.currentThread().interrupt();
}
Constructor and Description |
---|
ExceptionProxy(Thread toInterrupt)
Creates an exception proxy that interrupts the given thread upon report of an exception.
|
Modifier and Type | Method and Description |
---|---|
void |
checkAndThrowException()
Checks whether an exception has been set via
reportError(Throwable) . |
void |
reportError(Throwable t)
Sets the exception and interrupts the target thread, if no other exception has occurred so
far.
|
public void reportError(Throwable t)
The exception is only set (and the interruption is only triggered), if no other exception was set before.
t
- The exception that occurredpublic void checkAndThrowException() throws Exception
reportError(Throwable)
. If yes, that
exception if re-thrown by this method.Exception
- This method re-throws the exception, if set.Copyright © 2014–2021 The Apache Software Foundation. All rights reserved.