public class Async extends Object
Async.await(future)
from within methods whose return type is
CompletionStage, CompletableFuture or subclasses of CompletableFuture.
Async.await(future)
won't block. It is semantically equivalent to call future.join()
But instead of blocking the code in instrumented to become a state machine that evolves as the futures passed
to await
are completed.
This is equivalent to use CompletableFuture composition methods (ex: thenApply, handle, thenCompose).
The advantage of using await
is that the code will resemble sequential blocking code.
Example:
import static com.ea.async.Async.await;
import static java.util.concurrent.CompletableFuture.completedFuture;*
public class Store
{
public CompletableFuture buyItem(String itemTypeId, int cost)
{
if(!await(bank.decrement(cost))) {
return completedFuture(false);
}
await(inventory.giveItem(itemTypeId));
return completedFuture(true);
}
}
Constructor and Description |
---|
Async() |
Modifier and Type | Method and Description |
---|---|
static <T,F extends CompletionStage<T>> |
await(F future)
This method will behave as a
CompletableFuture.join() but will actually cause the
caller to return a promise instead of blocking. |
static void |
init()
Ensure that if no pre instrumentation was done, that the Async runtime instrumentation is running.
|
public static void init()
public static <T,F extends CompletionStage<T>> T await(F future)
CompletableFuture.join()
but will actually cause the
caller to return a promise instead of blocking.
Calls to this method are replaced by the EA Async instrumentation.T
- the future value type.future
- a future to wait for.CompletionException
- wrapping the actual exception if an exception occured.Copyright © 2019 Electronic Arts Inc. All rights reserved.