MCPcopy Create free account
hub / github.com/traneio/future / FuturePool

Class FuturePool

future-java/src/main/java/io/trane/future/FuturePool.java:38–78  ·  view source on GitHub ↗

A `FuturePool` isolates portions of a `Future` composition on a separate thread pool. Example: ```java FuturePool futurePool = FuturePool.apply(Executors.newCachedThreadPool()); Future > user = documentService.get(docId) .flatMap(doc -> futurePool.async(tokenize(doc))) ``` This

Source from the content-addressed store, hash-verified

36 * `isolate` is just a shortcut for `async` + `Future.flatten`.
37 */
38public final class FuturePool {
39
40 private final ExecutorService executor;
41
42 /**
43 * Creates a new future pool.
44 * @param executor the executor used to schedule tasks.
45 * @return the new future pool.
46 */
47 public static FuturePool apply(final ExecutorService executor) {
48 return new FuturePool(executor);
49 }
50
51 private FuturePool(final ExecutorService executor) {
52 this.executor = executor;
53 }
54
55 /**
56 * Isolates the execution of a future on this future pool.
57 * @param s the supplier that creates the future.
58 * @return the isolated future.
59 */
60 public final <T> Future<T> isolate(final Supplier<Future<T>> s) {
61 return Future.flatten(async(s));
62 }
63
64 /**
65 * Isolates the execution of the supplier on this future pool.
66 * @param s the supplier.
67 * @return a future with the result of the supplier.
68 */
69 public final <T> Future<T> async(final Supplier<T> s) {
70 try {
71 final AsyncPromise<T> p = new AsyncPromise<>(s);
72 executor.submit(p);
73 return p;
74 } catch (final RejectedExecutionException ex) {
75 return Future.exception(ex);
76 }
77 }
78}
79
80final class AsyncPromise<T> extends Promise<T> implements Runnable {
81 private final Supplier<T> s;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…