Class FutureImpl<T>
- Type Parameters:
T
- Result of the computation.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionThe queue of actions is filled when calling onComplete() before the Future is completed or cancelled.private boolean
Indicates if this Future is cancelled GuardedBy("lock")private final Executor
Used to start new threads.private final Object
Used to synchronize state changes.private Thread
The Thread which runs the computation.Once the Future is completed, the value is defined.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 -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Blocks the current thread.(package private) static <T> FutureImpl
<T> Creates aFutureImpl
that is eventually completed.await()
Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.Blocks the current Thread until this Future completed or returns immediately if this Future is already completed.boolean
cancel
(boolean mayInterruptIfRunning) Cancels the Future.executor()
Returns theExecutor
used by thisFuture
.Deprecated.getValue()
Returns the value of the Future.private void
boolean
Checks if this Future is cancelled, i.e.boolean
Checks if this Future is completed, i.e.(package private) static <T> FutureImpl
<T> Creates aFutureImpl
that needs to be automatically completed by callingtryComplete(Try)
.(package private) static <T> FutureImpl
<T> Creates aFutureImpl
that is immediately completed with the given value.onComplete
(Consumer<? super Try<T>> action) Performs the action once the Future is complete.private void
(package private) static <T> FutureImpl
<T> Creates aFutureImpl
that is eventually completed.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
private void
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 Details
-
executor
Used to start new threads. -
lock
Used to synchronize state changes. -
cancelled
private volatile boolean cancelledIndicates if this Future is cancelled GuardedBy("lock") -
value
Once the Future is completed, the value is defined. GuardedBy("lock") -
actions
The queue of actions is filled when calling onComplete() before the Future is completed or cancelled. Otherwise actions = null. GuardedBy("lock") -
waiters
The queue of waiters is filled when calling await() before the Future is completed or cancelled. Otherwise waiters = null. GuardedBy("lock") -
thread
The Thread which runs the computation. GuardedBy("lock")
-
-
Constructor Details
-
FutureImpl
-
-
Method Details
-
of
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
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
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
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
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
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
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)
. -
updateThread
private void updateThread() -
executor
Description copied from interface:Future
Returns theExecutor
used by thisFuture
. -
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
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
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
Description copied from interface:Value
Clarifies that values have a proper toString() method implemented.See Object.toString().
-
tryComplete
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:
IllegalStateException
- if the Future is already completed or cancelled.NullPointerException
- if the givenvalue
is null.
-
perform
-
unlock
-
handleUncaughtException
-