Class FutureImpl<T>
- java.lang.Object
-
- io.vavr.concurrent.FutureImpl<T>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static interface
FutureImpl.Computation<T>
-
Field Summary
Fields Modifier and Type Field Description 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.private boolean
cancelled
Indicates if this Future is cancelled GuardedBy("lock")private java.util.concurrent.Executor
executor
Used to start new threads.private java.util.concurrent.locks.Lock
lock
Used to synchronize state changes.private java.lang.Thread
thread
The Thread which runs the computation.private Option<Try<T>>
value
Once the Future is completed, the value is defined.private Queue<java.lang.Thread>
waiters
The queue of waiters is filled when calling await() before the Future is completed or cancelled.-
Fields inherited from interface io.vavr.concurrent.Future
DEFAULT_EXECUTOR, DEFAULT_EXECUTOR_SERVICE
-
-
Constructor Summary
Constructors Modifier Constructor Description private
FutureImpl(java.util.concurrent.Executor executor, Option<Try<T>> value, Queue<java.util.function.Consumer<Try<T>>> actions, Queue<java.lang.Thread> waiters, FutureImpl.Computation<T> computation)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private void
_await(long start, long timeout, java.util.concurrent.TimeUnit unit)
Blocks the current thread.(package private) static <T> FutureImpl<T>
async(java.util.concurrent.Executor executor, Task<? extends T> task)
Creates aFutureImpl
that is eventually completed.Future<T>
await()
Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.Future<T>
await(long timeout, java.util.concurrent.TimeUnit unit)
Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.boolean
cancel(boolean mayInterruptIfRunning)
Cancels the Future.java.util.concurrent.Executor
executor()
Returns theExecutor
used by thisFuture
.java.util.concurrent.ExecutorService
executorService()
Deprecated.Option<Try<T>>
getValue()
Returns the value of the Future.private void
handleUncaughtException(java.lang.Throwable x)
boolean
isCancelled()
Checks if this Future is cancelled, i.e.boolean
isCompleted()
Checks if this Future is completed, i.e.(package private) static <T> FutureImpl<T>
of(java.util.concurrent.Executor executor)
Creates aFutureImpl
that needs to be automatically completed by callingtryComplete(Try)
.(package private) static <T> FutureImpl<T>
of(java.util.concurrent.Executor executor, Try<? extends T> value)
Creates aFutureImpl
that is immediately completed with the given value.Future<T>
onComplete(java.util.function.Consumer<? super Try<T>> action)
Performs the action once the Future is complete.private void
perform(java.util.function.Consumer<? super Try<T>> action)
(package private) static <T> FutureImpl<T>
sync(java.util.concurrent.Executor executor, Task<? extends T> task)
Creates aFutureImpl
that is eventually completed.java.lang.String
toString()
Clarifies that values have a proper toString() method implemented.(package private) boolean
tryComplete(Try<? extends T> value)
INTERNAL METHOD, SHOULD BE USED BY THE CONSTRUCTOR, ONLY.private void
unlock(java.lang.Thread waiter)
private void
updateThread()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.vavr.concurrent.Future
andThen, cancel, collect, failed, fallbackTo, filter, filterTry, flatMap, flatMapTry, forEach, get, getCause, isAsync, isEmpty, isFailure, isLazy, isSingleValued, isSuccess, iterator, map, mapTry, onFailure, onSuccess, orElse, orElse, peek, recover, recoverWith, stringPrefix, toCompletableFuture, transform, transformValue, zip, zipWith
-
Methods inherited from interface io.vavr.Value
collect, collect, contains, corresponds, eq, equals, exists, forAll, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, hashCode, out, out, spliterator, stderr, stdout, toArray, toCharSeq, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
-
-
-
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")
-
-
Method Detail
-
of
static <T> FutureImpl<T> of(java.util.concurrent.Executor executor)
Creates aFutureImpl
that needs to be automatically completed by callingtryComplete(Try)
.- Type Parameters:
T
- value type of the Future- Parameters:
executor
- AnExecutor
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 aFutureImpl
that is immediately completed with the given value. No task will be started.- Type Parameters:
T
- value type of the Future- Parameters:
executor
- AnExecutor
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 aFutureImpl
that is eventually completed. The givencomputation
is synchronously executed, no thread is started.- Type Parameters:
T
- value type of the Future- Parameters:
executor
- AnExecutor
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 aFutureImpl
that is eventually completed. The givencomputation
is asynchronously executed, a new thread is started.- Type Parameters:
T
- value type of the Future- Parameters:
executor
- AnExecutor
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 correspondingInterruptedException
.
-
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 correspondingInterruptedException
.If the deadline wasn't met, a failed
Future
is returned containing aTimeoutException
.
-
_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), otherwiseLockSupport.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 givenunit
of timeunit
- 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 underlyingExecutor
.If the Future was successfully cancelled, the result is a
Failure(CancellationException)
.- Specified by:
cancel
in interfaceFuture<T>
- Parameters:
mayInterruptIfRunning
-true
if a running thread should be interrupted, otherwise a running thread is allowed to complete its computation.- Returns:
false
, if thisFuture
is already completed or could not be cancelled, otherwisetrue
.- See Also:
Future.isCancelled()
,Future.cancel(boolean)
-
updateThread
private void updateThread()
-
executor
public java.util.concurrent.Executor executor()
Description copied from interface:Future
Returns theExecutor
used by thisFuture
.
-
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 ANUnsupportedOperationException
AT RUNTIME.- Specified by:
executorService
in interfaceFuture<T>
- Returns:
- (never)
-
getValue
public Option<Try<T>> getValue()
Description copied from interface:Future
Returns the value of the Future.
-
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 interfaceFuture<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 interfaceFuture<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 interfaceFuture<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().
-
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 givenvalue
is null.
-
unlock
private void unlock(java.lang.Thread waiter)
-
handleUncaughtException
private void handleUncaughtException(java.lang.Throwable x)
-
-