Class ResultExchange<T>
- java.lang.Object
-
- de.aristaflow.adept2.util.threading.ResultExchange<T>
-
- Type Parameters:
T- The type of the result.
- All Implemented Interfaces:
Future<T>
public class ResultExchange<T> extends Object implements Future<T>
This class provides the means to exchange a result between different threads. It resembles aFutureexcept for the possibility to cancel it.
The implementation is based on aCountDownLatch. This is awaited by the result readers and signalled by the writer.When using it make sure to call
endComputation(Object)orfailComputation(Throwable)under all circumstances after making the instance ofResultExchangeavailable 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); }
-
-
Field Summary
Fields Modifier and Type Field Description protected booleandoneWhether the result has been computed (or failed to compute).protected CountDownLatchlatchThe latch for signalling the result being available.protected TresultThe result after computation.protected ThrowablethrowableThe throwable which caused the computation to fail.
-
Constructor Summary
Constructors Constructor Description ResultExchange()Creates a new result exchange.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancancel(boolean mayInterruptIfRunning)voidendComputation(T res)Computation has ended with the designated result to be provided by thisResultExchange.voidfailComputation(Throwable t)Computation has failed with the designated throwable to be provided by thisResultExchangeunless the designated throwable isnull.Tget()Tget(long timeout, TimeUnit unit)booleanisCancelled()booleanisDone()
-
-
-
Field Detail
-
latch
protected final CountDownLatch latch
The latch for signalling the result being available.
-
done
protected volatile boolean done
Whether the result has been computed (or failed to compute).
-
result
protected T result
The result after computation.
-
throwable
protected Throwable throwable
The throwable which caused the computation to fail.
-
-
Method Detail
-
endComputation
public void endComputation(T res)
Computation has ended with the designated result to be provided by thisResultExchange.- Parameters:
res- The result to be provided by thisResultExchange
-
failComputation
public void failComputation(Throwable t)
Computation has failed with the designated throwable to be provided by thisResultExchangeunless the designated throwable isnull. This helps signalling the computation within afinally.- Parameters:
t- The throwable to be provided instead of a result by thisResultExchange. Usenullto indicate no failure.
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelledin interfaceFuture<T>
-
get
public T get() throws InterruptedException, ExecutionException
- Specified by:
getin interfaceFuture<T>- Throws:
InterruptedExceptionExecutionException
-
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
getin interfaceFuture<T>- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
-