Interface Future<T>
- Type Parameters:
T- result of the computation
- All Known Implementing Classes:
FutureImpl
This type is an abstraction of a computation executed in another thread. To run the computation an Executor
should be provided. If no Executor is provided, then a default instance is used.
The result of the computation is a Try<T>, this means that the computation can end successfully or with an error.
You can create instances of Future in this way:
- Future.success(value): returns a future that returns successfully with the given value.
- Future.failure(error): returns a future that returns an error with the given error.
- Future.task(computation): returns a future that eventually will execute the given computation.
- Future.async(consumer): returns a future that eventually will consume the result of a computation.
- Future.exec(runnable): returns a future that eventually will execute the given runnable.
- Future.delay(duration, computation): returns a future that eventually will execute the given computation, but after waiting the given duration.
- Future.defer(computation): returns a future that eventually will execute the given computation that returns another Future.
- Future.later(computation): returns a future that eventually will execute the given computation that returns another value.
- Future.bracket(acquire, usage, release): returns a future that eventually will acquire a resource, then use it, and finally release it.
A future can be cancellable by calling the method cancel. If the future has not been executed yet, the future will be cancelled
and the result of the computation will be a Try.failure(CancellableException), but if the future has been executed, and is completed
the calling of cancel method will not have any consequences. If the computation is running when the cancel method is called, and if the flag
mayInterruptThread is true, then it will try to interrupt the thread running the computation and the result of the computation
will be also a Try.failure(CancellableException).
If during the execution of the computation in a thread, this thread is interrupted for any reason, the result
of the computation will be a Try.failure(InterruptedException).
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescription<R> Future<R> <R> Future<R> static <T> Future<T> static <T> Future<T> await()static <T extends AutoCloseable,R>
Future<R> static <T,R> Future <R> bracket(Future<? extends T> acquire, Function1<? super T, ? extends Future<? extends R>> use, Consumer1<? super T> release) static <T extends AutoCloseable,R>
Future<R> bracket(Executor executor, Future<? extends T> acquire, Function1<? super T, ? extends Future<? extends R>> use) static <T,R> Future <R> bracket(Executor executor, Future<? extends T> acquire, Function1<? super T, ? extends Future<? extends R>> use, Consumer1<? super T> release) voidcancel(boolean mayInterruptThread) static <T> Future<T> static <T> Future<T> exec(CheckedRunnable task) exec(Executor executor, CheckedRunnable task) static <T> Future<T> static <T> Future<T> <R> Future<R> <U> Future<U> fold(Function1<? super Throwable, ? extends U> failureMapper, Function1<? super T, ? extends U> successMapper) static <T> Future<T> static <T> Future<T> static <T> Future<T> from(CompletableFuture<? extends T> future) static <T> Future<T> static <T> Future<T> static <T> Future<T> from(Executor executor, CompletableFuture<? extends T> future) static <T> Future<T> static <T> Future<T> default Tget()default ThrowablegetCause()default Tdefault Tdefault TgetOrElseThrow(Producer<? extends X> producer) booleanbooleanstatic <T> Future<T> static <T> Future<T> <R> Future<R> static <T,V, R> Future <R> map2(Future<? extends T> fa, Future<? extends V> fb, Function2<? super T, ? super V, ? extends R> mapper) onComplete(Consumer1<? super Try<? extends T>> callback) recoverWith(Class<X> type, Function1<? super X, ? extends T> mapper) static <T> Future<T> static <T> Future<T> success(T value) static <T> Future<T> static <T> Future<T> default CompletableFuture<T>
-
Field Details
-
DEFAULT_EXECUTOR
-
-
Method Details
-
await
-
await
-
cancel
void cancel(boolean mayInterruptThread) -
isCompleted
boolean isCompleted() -
isCancelled
boolean isCancelled() -
onSuccess
-
onFailure
-
onComplete
-
map
-
mapError
-
flatMap
-
andThen
-
ap
-
filter
-
filterNot
-
orElse
-
get
-
getOrElse
-
getOrElse
-
getOrElseThrow
-
getOrElseThrow
- Throws:
X
-
getCause
-
recover
-
recoverWith
-
fold
-
toCompletableFuture
-
toPromise
-
success
-
success
-
failure
-
failure
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
from
-
task
-
task
-
exec
-
exec
-
delay
-
delay
-
sleep
-
sleep
-
later
-
later
-
bracket
static <T extends AutoCloseable,R> Future<R> bracket(Future<? extends T> acquire, Function1<? super T, ? extends Future<? extends R>> use) -
bracket
-
bracket
-
bracket
-
traverse
-
map2
-
tuple
-
async
-
async
-