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 booleanIndicates if this Future is cancelled GuardedBy("lock")private final ExecutorUsed to start new threads.private final LockUsed to synchronize state changes.private ThreadThe 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 Future
DEFAULT_EXECUTOR, DEFAULT_EXECUTOR_SERVICE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidBlocks the current thread.(package private) static <T> FutureImpl<T> Creates aFutureImplthat 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.booleancancel(boolean mayInterruptIfRunning) Cancels the Future.executor()Returns theExecutorused by thisFuture.Deprecated.getValue()Returns the value of the Future.private voidbooleanChecks if this Future is cancelled, i.e.booleanChecks if this Future is completed, i.e.(package private) static <T> FutureImpl<T> Creates aFutureImplthat needs to be automatically completed by callingtryComplete(Try).(package private) static <T> FutureImpl<T> Creates aFutureImplthat 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 aFutureImplthat is eventually completed.toString()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 voidprivate voidMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface 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, zipWithMethods inherited from interface 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
-
actions
-
waiters
-
thread
The Thread which runs the computation. GuardedBy("lock")
-
-
Constructor Details
-
FutureImpl
-
-
Method Details
-
of
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
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
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
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
Description copied from interface:FutureBlocks 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
Futureis returned containing the correspondingInterruptedException. -
await
Description copied from interface:FutureBlocks 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
Futureis returned containing the correspondingInterruptedException.If the deadline wasn't met, a failed
Futureis 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 givenunitof timeunit- a time unit
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) Description copied from interface:FutureCancels 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
-
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
-
isCancelled
public boolean isCancelled()Description copied from interface:FutureChecks if this Future is cancelled, i.e. the thread was forced to stop before completion.- Specified by:
isCancelledin interfaceFuture<T>- Returns:
- true, if the computation was cancelled, false otherwise
-
isCompleted
public boolean isCompleted()Description copied from interface:FutureChecks if this Future is completed, i.e. has a value.- Specified by:
isCompletedin interfaceFuture<T>- Returns:
- true, if the computation successfully finished, failed or was cancelled, false otherwise.
-
onComplete
-
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 givenvalueis null.
-
perform
-
unlock
-
handleUncaughtException
-