Interface Future<T>

Type Parameters:
T - result of the computation
All Superinterfaces:
Bindable<Future<?>,T>, FutureOf<T>, Kind<Future<?>,T>, Mappable<Future<?>,T>
All Known Implementing Classes:
FutureImpl

public sealed interface Future<T> extends FutureOf<T>, Bindable<Future<?>,T> permits FutureImpl<T>

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: