public class Await extends Object
await(future)
will cause the method to return a CompletableFuture (or Task) instead of blocking.
This is equivalent to use CompletableFuture composition methods (ex: thenApply, handle).
The advantage of using await is that the code will resemble sequential blocking code.
Example:
import com.ea.orbit.async.Async;
import static com.ea.orbit.async.Await.await;
...
Async
CompletableFuture getPageLengthAsync()
{
CompletableFuture pageFuture = getPageAsync("http://example.com");
String page = await(pageFuture);
return CompletableFuture.completedFuture(page.length);
}
Or using orbit Task:
Async
Task CompletableFuture getPageLengthAsync()
{
Task pageFuture = getPageAsync("http://example.com");
String page = await(pageFuture);
return Task.fromValue(page.length);
}
Caveat: The following code must be called before the program execution:
static { Await.init() }
Otherwise, the first method to call await() might be blocking,
and a warning message will be printed to the console.
Subsequent async methods will work as expected.| Constructor and Description |
|---|
Await() |
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
await(CompletableFuture<T> future)
Calls to this method are replaced by the orbit-async instrumentation.
|
static void |
init() |
public static void init()
public static <T> T await(CompletableFuture<T> future)
T - the return type of future.join()future - a future to wait for.Copyright © 2015 Electronic Arts Inc. All rights reserved.