public final class FuturePool extends Object
A FuturePool isolates portions of a Future composition on a separate thread pool.
Example:
FuturePool futurePool = FuturePool.apply(Executors.newCachedThreadPool());
Future<List<Token>> user =
documentService.get(docId)
.flatMap(doc -> futurePool.async(tokenize(doc)))
This feature useful to isolate cpu-intensive tasks and blocking operations. Please refer to the Java documentation to decide which type of executor is the best for the kind of task that needs to be performed. For instance, a ForkJoinPool is useful for cpu-intensive tasks, but can’t be used for blocking operations.
The FuturePool also has the method isolate that isolates the execution of a Future:
FuturePool futurePool = FuturePool.apply(Executors.newCachedThreadPool());
Future<User> user = futurePool.isolate(userRepo.get(userId));
isolate is just a shortcut for async + Future.flatten.
| Modifier and Type | Method and Description |
|---|---|
static FuturePool |
apply(ExecutorService executor)
Creates a new future pool.
|
<T> Future<T> |
async(java.util.function.Supplier<T> s)
Isolates the execution of the supplier on this future pool.
|
<T> Future<T> |
isolate(java.util.function.Supplier<Future<T>> s)
Isolates the execution of a future on this future pool.
|
public static FuturePool apply(ExecutorService executor)
Creates a new future pool.
executor - the executor used to schedule tasks.public final <T> Future<T> isolate(java.util.function.Supplier<Future<T>> s)
Isolates the execution of a future on this future pool.
s - the supplier that creates the future.public final <T> Future<T> async(java.util.function.Supplier<T> s)
Isolates the execution of the supplier on this future pool.
s - the supplier.Copyright © 2017. All Rights Reserved.