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 a Future except for the possibility to cancel it.
The implementation is based on a 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);
 }
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
    Whether the result has been computed (or failed to compute).
    protected final CountDownLatch
    The latch for signalling the result being available.
    protected T
    The result after computation.
    protected Throwable
    The throwable which caused the computation to fail.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new result exchange.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    cancel(boolean mayInterruptIfRunning)
     
    void
    Computation has ended with the designated result to be provided by this ResultExchange.
    void
    Computation has failed with the designated throwable to be provided by this ResultExchange unless the designated throwable is null.
    get()
     
    get(long timeout, TimeUnit unit)
     
    boolean
     
    boolean
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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.
  • Constructor Details

    • ResultExchange

      public ResultExchange()
      Creates a new result exchange.
  • Method Details