Class 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 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.
    • Constructor Detail

      • ResultExchange

        public ResultExchange()
        Creates a new result exchange.