public class Future<E> extends Object
Represents long-running operation that should be executed asynchronously.
Result of the operation is either a value or a Throwable. To get the result of operation
caller should supply handlers for success and/or failure.
Several operations can be chained by using then(Func) method, for
example:
Future.run(callable).then(transform1).then(transform2).onSuccess(ok).onFailure(boom);
This operation can fail at any time, but will never allow any exception escape. In the event that every
callable and transform completes successfully then onSuccess(Procedure)
will be called, otherwise onFailure(Procedure) will be called with
and exception as parameter. All the operations will be performed asynchronously.| Constructor and Description |
|---|
Future()
Creates a future with a default executor
|
Future(ExecutorService executor)
Creates a future with supplied executor service
|
| Modifier and Type | Method and Description |
|---|---|
Future<E> |
doIt(Callable<E> callable)
Executes block of code in the future.
|
Try<E> |
getResult()
Returns the result of the Future.
|
boolean |
isCompleted()
Checks if execution is complete
|
Future<E> |
onFailure(Procedure<Throwable> onFailure)
Sets failed execution handler.
|
Future<E> |
onSuccess(Procedure<E> onSuccess)
Sets successful execution handler.
|
static <E> Future<E> |
run(Callable<E> block)
Shortcut for executing a
Callable with default parameters. |
<T> Future<T> |
then(Func<T,E> transform)
Allows chaining asynchronous operations.
|
public Future()
public Future(ExecutorService executor)
executor - ExecutorService to be used for asynchronous executionpublic static <E> Future<E> run(Callable<E> block)
Shortcut for executing a Callable with default parameters.
Will create a new Future and execute it.
E - type of the resultblock - callable to be executedpublic final <T> Future<T> then(Func<T,E> transform)
Allows chaining asynchronous operations.
Will start new supplied transform function within a new Future as soon as this future succeeds. In case this future fails, failure cause will be propagated to the new Future.
T - type of the new future resulttransform - function to transform results of this future into result of a new futurepublic final Future<E> onSuccess(Procedure<E> onSuccess)
Sets successful execution handler.
This handler can be set only once. Will throw IllegalStateException if attempt to set
it again will be made
There are no guarantees when the callback will be called
onSuccess - Procedure to handle successful Future executionpublic final Future<E> onFailure(Procedure<Throwable> onFailure)
Sets failed execution handler.
This handler can be set only once. Will throw IllegalStateException if attempt to set
it again will be made
There are no guarantees when the callback will be called
onFailure - Procedure to handle failed Future executionpublic final Future<E> doIt(Callable<E> callable)
Executes block of code in the future.
Each future can execute code only once. Second attempt ot call this method will throw
IllegalStateException
callable - Callable that will be executed asynchronouslypublic Try<E> getResult()
Try so that exceptions during future execution are captured.public boolean isCompleted()
true if future is completed, false otherwiseCopyright © 2018. All rights reserved.