// We'll allow a 2-sized thread pool for async execution, rather than using the fork/join pool for asynchronous // value generation finalExecutorexecutor= Executors.newFixedThreadPool(2);
@Test publicvoiddemoThenApply()throws Exception { final CompletableFuture<Integer> future = CompletableFuture .supplyAsync(delayedValueSupplier(1), executor) .thenApply(i -> i + 3); assertThat(future.get(), is(4)); }
@Test publicvoiddemoMultiStagePipeline()throws Exception { // We'll use one future to specify how long a subsequent future will take final CompletableFuture<Integer> future = CompletableFuture .supplyAsync(delayedValueSupplier(3), executor) .thenApply(i -> { System.out.println("First future completed, " + i); return i + 1; }) .thenCompose(i -> CompletableFuture .supplyAsync(delayedValueSupplier(i + 2, i * 100), executor)); assertThat(future.get(), is(6)); }