Java Code Examples for java.util.concurrent.CompletableFuture#thenAcceptBothAsync()
The following examples show how to use
java.util.concurrent.CompletableFuture#thenAcceptBothAsync() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DependencyTaskExecutor.java From interview with Apache License 2.0 | 5 votes |
void scheduleTask(List<Task> tasks, int threads) { ExecutorService executor = Executors.newFixedThreadPool(threads); CompletableFuture<Void> future = CompletableFuture.completedFuture(null); for (Task task : tasks) { future = future.thenAcceptBothAsync(scheduleTaskUtil(task, executor), (a, b) -> {}, executor); } future.thenRunAsync(() -> {System.out.println("All tasks done. Closing executor"); executor.shutdown();}); }
Example 2
Source File: ThreadContextTest.java From microprofile-context-propagation with Apache License 2.0 | 4 votes |
/** * Verify that the ThreadContext's contextualConsumer * method can be used to wrap a BiConsumer instance with the context that is captured from the * current thread per the configuration of the ThreadContext builder, and that the context is * applied when the BiConsumer accept method runs. This test case aligns with use case of * supplying a contextual BiConsumer to a completion stage that is otherwise not context-aware. * * @throws InterruptedException indicates test failure */ @Test public void contextualBiConsumerRunsWithContext() throws InterruptedException { ThreadContext bufferContext = ThreadContext.builder() .propagated(Buffer.CONTEXT_NAME) .unchanged() .cleared(ThreadContext.ALL_REMAINING) .build(); try { // Set non-default values Buffer.get().append("contextualBiConsumer-test-buffer"); Label.set("contextualBiConsumer-test-label"); // To avoid the possibility that CompletableFuture.get might cause the action to run // on the current thread, which would bypass the intent of testing context propagation, // use a countdown latch to independently wait for completion. CountDownLatch completed = new CountDownLatch(1); // CompletableFuture from Java SE is intentionally used here to avoid the context // propagation guarantees of ManagedExecutor. // This ensures we are testing that ThreadContext is // doing the work to propagate the context rather than getting it from a // ManagedExecutor. CompletableFuture<String> stage1a = CompletableFuture.supplyAsync(() -> "supplied-value-A"); CompletableFuture<String> stage1b = CompletableFuture.supplyAsync(() -> "supplied-value-B"); CompletableFuture<Void> stage2 = stage1a.thenAcceptBothAsync(stage1b, bufferContext.contextualConsumer((a, b) -> { Assert.assertEquals(a, "supplied-value-A", "First value supplied to BiConsumer was lost or altered."); Assert.assertEquals(b, "supplied-value-B", "Second value supplied to BiConsumer was lost or altered."); Assert.assertEquals(Buffer.get().toString(), "contextualBiConsumer-test-buffer", "Context type was not propagated to contextual action."); Assert.assertEquals(Label.get(), "", "Context type that is configured to be cleared was not cleared."); }), unmanagedThreads); stage2.whenComplete((unused, failure) -> completed.countDown()); Assert.assertTrue(completed.await(MAX_WAIT_NS, TimeUnit.NANOSECONDS), "Completable future did not finish in a reasonable amount of time."); // Force errors, if any, to be reported stage2.join(); } finally { // Restore original values Buffer.set(null); Label.set(null); } }
Example 3
Source File: CompletableFutureTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public <T,U> CompletableFuture<Void> thenAcceptBoth (CompletableFuture<T> f, CompletionStage<? extends U> g, BiConsumer<? super T,? super U> a) { return f.thenAcceptBothAsync(g, a); }
Example 4
Source File: CompletableFutureTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public <T,U> CompletableFuture<Void> thenAcceptBoth (CompletableFuture<T> f, CompletionStage<? extends U> g, BiConsumer<? super T,? super U> a) { return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); }
Example 5
Source File: CompletableFutureTest.java From j2objc with Apache License 2.0 | 4 votes |
public <T,U> CompletableFuture<Void> thenAcceptBoth (CompletableFuture<T> f, CompletionStage<? extends U> g, BiConsumer<? super T,? super U> a) { return f.thenAcceptBothAsync(g, a); }
Example 6
Source File: CompletableFutureTest.java From j2objc with Apache License 2.0 | 4 votes |
public <T,U> CompletableFuture<Void> thenAcceptBoth (CompletableFuture<T> f, CompletionStage<? extends U> g, BiConsumer<? super T,? super U> a) { return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); }