Class FutureImpl<T>

  • Type Parameters:
    T - Result of the computation.
    All Implemented Interfaces:
    Future<T>, Value<T>, java.lang.Iterable<T>

    final class FutureImpl<T>
    extends java.lang.Object
    implements Future<T>
    INTERNAL API - This class is subject to change.
    • Field Detail

      • executor

        private final java.util.concurrent.Executor executor
        Used to start new threads.
      • lock

        private final java.util.concurrent.locks.Lock lock
        Used to synchronize state changes.
      • cancelled

        private volatile boolean cancelled
        Indicates if this Future is cancelled GuardedBy("lock")
      • value

        private volatile Option<Try<T>> value
        Once the Future is completed, the value is defined. GuardedBy("lock")
      • actions

        private Queue<java.util.function.Consumer<Try<T>>> actions
        The queue of actions is filled when calling onComplete() before the Future is completed or cancelled. Otherwise actions = null. GuardedBy("lock")
      • waiters

        private Queue<java.lang.Thread> waiters
        The queue of waiters is filled when calling await() before the Future is completed or cancelled. Otherwise waiters = null. GuardedBy("lock")
      • thread

        private java.lang.Thread thread
        The Thread which runs the computation. GuardedBy("lock")
    • Constructor Detail

    • Method Detail

      • of

        static <T> FutureImpl<T> of​(java.util.concurrent.Executor executor)
        Creates a FutureImpl that needs to be automatically completed by calling tryComplete(Try).
        Type Parameters:
        T - value type of the Future
        Parameters:
        executor - An Executor to run and control the computation and to perform the actions.
        Returns:
        a new FutureImpl instance
      • of

        static <T> FutureImpl<T> of​(java.util.concurrent.Executor executor,
                                    Try<? extends T> value)
        Creates a FutureImpl that is immediately completed with the given value. No task will be started.
        Type Parameters:
        T - value type of the Future
        Parameters:
        executor - An Executor to run and control the computation and to perform the actions.
        value - the result of this Future
        Returns:
        a new FutureImpl instance
      • sync

        static <T> FutureImpl<T> sync​(java.util.concurrent.Executor executor,
                                      Task<? extends T> task)
        Creates a FutureImpl that is eventually completed. The given computation is synchronously executed, no thread is started.
        Type Parameters:
        T - value type of the Future
        Parameters:
        executor - An Executor to run and control the computation and to perform the actions.
        task - A non-blocking computation
        Returns:
        a new FutureImpl instance
      • async

        static <T> FutureImpl<T> async​(java.util.concurrent.Executor executor,
                                       Task<? extends T> task)
        Creates a FutureImpl that is eventually completed. The given computation is asynchronously executed, a new thread is started.
        Type Parameters:
        T - value type of the Future
        Parameters:
        executor - An Executor to run and control the computation and to perform the actions.
        task - A (possibly blocking) computation
        Returns:
        a new FutureImpl instance
      • await

        public Future<T> await()
        Description copied from interface: Future
        Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.

        In the case the current thread was interrupted while waiting, a failed Future is returned containing the corresponding InterruptedException.

        Specified by:
        await in interface Future<T>
        Returns:
        this Future instance
      • await

        public Future<T> await​(long timeout,
                               java.util.concurrent.TimeUnit unit)
        Description copied from interface: Future
        Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.

        In the case the current thread was interrupted while waiting, a failed Future is returned containing the corresponding InterruptedException.

        If the deadline wasn't met, a failed Future is returned containing a TimeoutException.

        Specified by:
        await in interface Future<T>
        Parameters:
        timeout - the maximum time to wait
        unit - the time unit of the timeout argument
        Returns:
        this Future instance
      • _await

        private void _await​(long start,
                            long timeout,
                            java.util.concurrent.TimeUnit unit)
        Blocks the current thread.

        If timeout = 0 then LockSupport.park() is called (start, timeout and unit are not used), otherwise LockSupport.park(timeout, unit} is called.

        If a timeout > -1 is specified and the deadline is not met, this Future fails with a TimeoutException.

        If this Thread was interrupted, this Future fails with a InterruptedException.

        Parameters:
        start - the start time in nanos, based on System.nanoTime()
        timeout - a timeout in the given unit of time
        unit - a time unit
      • cancel

        public boolean cancel​(boolean mayInterruptIfRunning)
        Description copied from interface: Future
        Cancels the Future. A pending Future may be interrupted, depending on the underlying Executor.

        If the Future was successfully cancelled, the result is a Failure(CancellationException).

        Specified by:
        cancel in interface Future<T>
        Parameters:
        mayInterruptIfRunning - true if a running thread should be interrupted, otherwise a running thread is allowed to complete its computation.
        Returns:
        false, if this Future is already completed or could not be cancelled, otherwise true.
        See Also:
        Future.isCancelled(), Future.cancel(boolean)
      • updateThread

        private void updateThread()
      • executor

        public java.util.concurrent.Executor executor()
        Description copied from interface: Future
        Returns the Executor used by this Future.
        Specified by:
        executor in interface Future<T>
        Returns:
        The underlying Executor.
      • executorService

        @Deprecated
        public java.util.concurrent.ExecutorService executorService()
        Deprecated.
        Description copied from interface: Future
        This method is deprecated.

        THE DEFAULT IMPLEMENTATION (obtained by one of the Future factory methods) MIGHT THROW AN UnsupportedOperationException AT RUNTIME.

        Specified by:
        executorService in interface Future<T>
        Returns:
        (never)
      • getValue

        public Option<Try<T>> getValue()
        Description copied from interface: Future
        Returns the value of the Future.
        Specified by:
        getValue in interface Future<T>
        Returns:
        None, if the Future is not yet completed or was cancelled, otherwise Some(Try).
      • isCancelled

        public boolean isCancelled()
        Description copied from interface: Future
        Checks if this Future is cancelled, i.e. the thread was forced to stop before completion.
        Specified by:
        isCancelled in interface Future<T>
        Returns:
        true, if the computation was cancelled, false otherwise
      • isCompleted

        public boolean isCompleted()
        Description copied from interface: Future
        Checks if this Future is completed, i.e. has a value.
        Specified by:
        isCompleted in interface Future<T>
        Returns:
        true, if the computation successfully finished, failed or was cancelled, false otherwise.
      • onComplete

        public Future<T> onComplete​(java.util.function.Consumer<? super Try<T>> action)
        Description copied from interface: Future
        Performs the action once the Future is complete.
        Specified by:
        onComplete in interface Future<T>
        Parameters:
        action - An action to be performed when this future is complete.
        Returns:
        this Future
      • toString

        public java.lang.String toString()
        Description copied from interface: Value
        Clarifies that values have a proper toString() method implemented.

        See Object.toString().

        Specified by:
        toString in interface Value<T>
        Overrides:
        toString in class java.lang.Object
        Returns:
        A String representation of this object
      • tryComplete

        boolean tryComplete​(Try<? extends T> value)
        INTERNAL METHOD, SHOULD BE USED BY THE CONSTRUCTOR, ONLY.

        Completes this Future with a value and performs all actions.

        This method is idempotent. I.e. it does nothing, if this Future is already completed.

        Parameters:
        value - A Success containing a result or a Failure containing an Exception.
        Throws:
        java.lang.IllegalStateException - if the Future is already completed or cancelled.
        java.lang.NullPointerException - if the given value is null.
      • perform

        private void perform​(java.util.function.Consumer<? super Try<T>> action)
      • unlock

        private void unlock​(java.lang.Thread waiter)
      • handleUncaughtException

        private void handleUncaughtException​(java.lang.Throwable x)