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 aFuture
except 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 ofResultExchange
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); }
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
done
Whether the result has been computed (or failed to compute).protected CountDownLatch
latch
The latch for signalling the result being available.protected T
result
The result after computation.protected Throwable
throwable
The 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 boolean
cancel(boolean mayInterruptIfRunning)
void
endComputation(T res)
Computation has ended with the designated result to be provided by thisResultExchange
.void
failComputation(Throwable t)
Computation has failed with the designated throwable to be provided by thisResultExchange
unless the designated throwable isnull
.T
get()
T
get(long timeout, TimeUnit unit)
boolean
isCancelled()
boolean
isDone()
-
-
-
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 thisResultExchange
unless the designated throwable isnull
. This helps signalling the computation within afinally
.- Parameters:
t
- The throwable to be provided instead of a result by thisResultExchange
. Usenull
to indicate no failure.
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelled
in interfaceFuture<T>
-
get
public T get() throws InterruptedException, ExecutionException
- Specified by:
get
in interfaceFuture<T>
- Throws:
InterruptedException
ExecutionException
-
get
public T get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
get
in interfaceFuture<T>
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
-