Class FutureImpl<T>
- java.lang.Object
-
- io.vavr.concurrent.FutureImpl<T>
-
-
Field Summary
Fields Modifier and Type Field Description private Queue<java.util.function.Consumer<Try<T>>>actionsThe queue of actions is filled when calling onComplete() before the Future is completed or cancelled.private booleancancelledIndicates if this Future is cancelled GuardedBy("lock")private java.util.concurrent.ExecutorexecutorUsed to start new threads.private java.util.concurrent.locks.LocklockUsed to synchronize state changes.private java.lang.ThreadthreadThe Thread which runs the computation.private Option<Try<T>>valueOnce the Future is completed, the value is defined.private Queue<java.lang.Thread>waitersThe 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
-
-
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 aFutureImplthat is eventually completed.Future<T>await()Blocks the current thread until thisFutureis completed, or returns immediately if it is already completed.Future<T>await(long timeout, @NonNull java.util.concurrent.TimeUnit unit)Blocks the current thread until thisFutureis completed, or returns immediately if it is already completed.booleancancel(boolean mayInterruptIfRunning)Cancels thisFuture.java.util.concurrent.Executorexecutor()Returns theExecutorthat executes asynchronous handlers for thisFuture.java.util.concurrent.ExecutorServiceexecutorService()Deprecated.Option<Try<T>>getValue()Returns the value of thisFuture.private voidhandleUncaughtException(java.lang.Throwable x)booleanisCancelled()Checks whether thisFuturewas cancelled, i.e., its computation was interrupted before completion.booleanisCompleted()Checks whether thisFutureis completed, i.e., whether it has finished with a value, failed, or was cancelled.(package private) static <T> FutureImpl<T>of(java.util.concurrent.Executor executor)Creates aFutureImplthat 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 aFutureImplthat is immediately completed with the given value.Future<T>onComplete(@NonNull java.util.function.Consumer<? super Try<T>> action)Performs the given action once thisFutureis complete.private voidperform(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 aFutureImplthat is eventually completed.java.lang.StringtoString()Clarifies that values have a proper toString() method implemented.(package private) booleantryComplete(Try<? extends T> value)INTERNAL METHOD, SHOULD BE USED BY THE CONSTRUCTOR, ONLY.private voidunlock(java.lang.Thread waiter)private voidupdateThread()-
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, mapTo, mapToVoid, 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 aFutureImplthat needs to be automatically completed by callingtryComplete(Try).- Type Parameters:
T- value type of the Future- Parameters:
executor- AnExecutorto run and control the computation and to perform the actions.- Returns:
- a new
FutureImplinstance
-
of
static <T> FutureImpl<T> of(java.util.concurrent.Executor executor, Try<? extends T> value)
Creates aFutureImplthat is immediately completed with the given value. No task will be started.- Type Parameters:
T- value type of the Future- Parameters:
executor- AnExecutorto run and control the computation and to perform the actions.value- the result of this Future- Returns:
- a new
FutureImplinstance
-
sync
static <T> FutureImpl<T> sync(java.util.concurrent.Executor executor, Task<? extends T> task)
Creates aFutureImplthat is eventually completed. The givencomputationis synchronously executed, no thread is started.- Type Parameters:
T- value type of the Future- Parameters:
executor- AnExecutorto run and control the computation and to perform the actions.task- A non-blocking computation- Returns:
- a new
FutureImplinstance
-
async
static <T> FutureImpl<T> async(java.util.concurrent.Executor executor, Task<? extends T> task)
Creates aFutureImplthat is eventually completed. The givencomputationis asynchronously executed, a new thread is started.- Type Parameters:
T- value type of the Future- Parameters:
executor- AnExecutorto run and control the computation and to perform the actions.task- A (possibly blocking) computation- Returns:
- a new
FutureImplinstance
-
await
public Future<T> await()
Description copied from interface:FutureBlocks the current thread until thisFutureis completed, or returns immediately if it is already completed.If the current thread is interrupted while waiting, a failed
Futureis returned containing the correspondingInterruptedException.
-
await
public Future<T> await(long timeout, @NonNull java.util.concurrent.TimeUnit unit)
Description copied from interface:FutureBlocks the current thread until thisFutureis completed, or returns immediately if it is already completed.If the current thread is interrupted while waiting, a failed
Futureis returned containing the correspondingInterruptedException.If the specified timeout is reached before completion, a failed
Futureis 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 givenunitof timeunit- a time unit
-
cancel
public boolean cancel(boolean mayInterruptIfRunning)
Description copied from interface:FutureCancels thisFuture. A pending future may be interrupted depending on the underlyingExecutor.If the future is successfully cancelled, its result becomes a
Failure(CancellationException).- Specified by:
cancelin interfaceFuture<T>- Parameters:
mayInterruptIfRunning-trueif a running thread should be interrupted;falseallows it to complete- Returns:
falseif thisFutureis already completed or could not be cancelled,trueotherwise- See Also:
Future.isCancelled(),Future.cancel(boolean)
-
updateThread
private void updateThread()
-
executor
public java.util.concurrent.Executor executor()
Description copied from interface:FutureReturns theExecutorthat executes asynchronous handlers for thisFuture.
-
executorService
@Deprecated public java.util.concurrent.ExecutorService executorService()
Deprecated.Description copied from interface:FutureThis method is deprecated.THE DEFAULT IMPLEMENTATION (obtained by one of the
Futurefactory methods) MIGHT THROW ANUnsupportedOperationExceptionAT RUNTIME.- Specified by:
executorServicein interfaceFuture<T>- Returns:
- (never)
-
getValue
public Option<Try<T>> getValue()
Description copied from interface:FutureReturns the value of thisFuture.
-
isCancelled
public boolean isCancelled()
Description copied from interface:FutureChecks whether thisFuturewas cancelled, i.e., its computation was interrupted before completion.- Specified by:
isCancelledin interfaceFuture<T>- Returns:
trueif the computation was cancelled,falseotherwise
-
isCompleted
public boolean isCompleted()
Description copied from interface:FutureChecks whether thisFutureis completed, i.e., whether it has finished with a value, failed, or was cancelled.- Specified by:
isCompletedin interfaceFuture<T>- Returns:
trueif the computation has completed in any state,falseotherwise
-
onComplete
public Future<T> onComplete(@NonNull java.util.function.Consumer<? super Try<T>> action)
Description copied from interface:FuturePerforms the given action once thisFutureis complete.- Specified by:
onCompletein interfaceFuture<T>- Parameters:
action- an action to execute when the future completes- Returns:
- this
Future
-
toString
public java.lang.String toString()
Description copied from interface:ValueClarifies 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 givenvalueis null.
-
unlock
private void unlock(java.lang.Thread waiter)
-
handleUncaughtException
private void handleUncaughtException(java.lang.Throwable x)
-
-