// concurrent/CompletableOperations.java// (c)2017 MindView LLC: see Copyright.txt// We make no guarantees that this code is fit for any purpose.// Visit http://OnJava8.com for more book information.import java.util.concurrent.*;import static onjava.CompletableUtilities.*;public class CompletableOperations {static CompletableFuture<Integer> cfi(int i) {returnCompletableFuture.completedFuture(Integer.valueOf(i));}public static void main(String[] args) {showr(cfi(1)); // Basic testvoidr(cfi(2).runAsync(() ->System.out.println("runAsync")));voidr(cfi(3).thenRunAsync(() ->System.out.println("thenRunAsync")));voidr(CompletableFuture.runAsync(() ->System.out.println("runAsync is static")));showr(CompletableFuture.supplyAsync(() -> 99));voidr(cfi(4).thenAcceptAsync(i ->System.out.println("thenAcceptAsync: " + i)));showr(cfi(5).thenApplyAsync(i -> i + 42));showr(cfi(6).thenComposeAsync(i -> cfi(i + 99)));CompletableFuture<Integer> c = cfi(7);c.obtrudeValue(111);showr(c);showr(cfi(8).toCompletableFuture());c = new CompletableFuture<>();c.complete(9);showr(c);c = new CompletableFuture<>();c.cancel(true);System.out.println("cancelled: " +c.isCancelled());System.out.println("completed exceptionally: " +c.isCompletedExceptionally());System.out.println("done: " + c.isDone());System.out.println(c);c = new CompletableFuture<>();System.out.println(c.getNow(777));c = new CompletableFuture<>();c.thenApplyAsync(i -> i + 42).thenApplyAsync(i -> i * 12);System.out.println("dependents: " +c.getNumberOfDependents());c.thenApplyAsync(i -> i / 2);System.out.println("dependents: " +c.getNumberOfDependents());}}/* Output:1runAsyncthenRunAsyncrunAsync is static99thenAcceptAsync: 44710511189cancelled: truecompleted exceptionally: truedone: truejava.util.concurrent.CompletableFuture@6d311334[Completed exceptionally]777dependents: 1dependents: 2*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。