1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| CompletableFuture<Integer> integerCompletableFuture1 = CompletableFuture.supplyAsync(() -> { System.out.println("当前1线程" + Thread.currentThread().getId()); int i = 10 / 2; System.out.println("运行结果" + i); return i; }, executor);
CompletableFuture<String> integerCompletableFuture2 = CompletableFuture.supplyAsync(() -> { System.out.println(2); return "hello"; }, executor);
integerCompletableFuture1.runAfterBoth(integerCompletableFuture2,()->{ System.out.println(3); },executor);
integerCompletableFuture1.thenAcceptBothAsync(integerCompletableFuture2,(f1,f2)->{ System.out.println(f1+f2); },executor);
CompletableFuture<String> stringCompletableFuture = integerCompletableFuture1.thenCombineAsync(integerCompletableFuture2, (f1, f2) -> { System.out.println(f1 + f2); return f1 + f2; }, executor);
System.out.println(stringCompletableFuture.get() );
|