T - The type of the result.public class ResultExchange<T>
extends java.lang.Object
implements java.util.concurrent.Future<T>
Future except for the possibility to cancel it. CountDownLatch. This is awaited by the result readers
and signalled by the writer.
When using it make sure to call endComputation(Object) or
failComputation(Throwable) under all circumstances after making the instance of
ResultExchange available to other threads, e. g.:
ResultExchange re;
Throwable t = new Throwable("Computation failed due to unknown reasons");
try
{
...
re.endComputation(...);
t = null;
}
catch (Exception e)
{
...
t = e;
}
finally
{
re.failComputation(t);
}
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
done
Whether the result has been computed (or failed to compute).
|
protected java.util.concurrent.CountDownLatch |
latch
The latch for signalling the result being available.
|
protected T |
result
The result after computation.
|
protected java.lang.Throwable |
throwable
The throwable which caused the computation to fail.
|
| Constructor and Description |
|---|
ResultExchange()
Creates a new result exchange.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
cancel(boolean mayInterruptIfRunning) |
void |
endComputation(T res)
Computation has ended with the designated result to be provided by this
ResultExchange. |
void |
failComputation(java.lang.Throwable t)
Computation has failed with the designated throwable to be provided by this
ResultExchange unless the designated throwable is null. |
T |
get() |
T |
get(long timeout,
java.util.concurrent.TimeUnit unit) |
boolean |
isCancelled() |
boolean |
isDone() |
protected final java.util.concurrent.CountDownLatch latch
protected volatile boolean done
protected T result
protected java.lang.Throwable throwable
public void endComputation(T res)
ResultExchange.res - The result to be provided by this ResultExchangepublic void failComputation(java.lang.Throwable t)
ResultExchange unless the designated throwable is null. This helps
signalling the computation within a finally.t - The throwable to be provided instead of a result by this ResultExchange.
Use null to indicate no failure.public boolean cancel(boolean mayInterruptIfRunning)
cancel in interface java.util.concurrent.Future<T>public boolean isCancelled()
isCancelled in interface java.util.concurrent.Future<T>public boolean isDone()
isDone in interface java.util.concurrent.Future<T>public T get() throws java.lang.InterruptedException, java.util.concurrent.ExecutionException
get in interface java.util.concurrent.Future<T>java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionpublic T get(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
get in interface java.util.concurrent.Future<T>java.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutException