本文共 1088 字,大约阅读时间需要 3 分钟。
Java CompletableFuture:thenCompose (3)
private void seq() throws ExecutionException, InterruptedException { CompletableFuturef1 = CompletableFuture.supplyAsync(new Supplier () { @Override public String get() { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return "2018"; } }); CompletableFuture f2 = f1.thenCompose(new Function >() { @Override public CompletableFuture apply(String s) { return CompletableFuture.supplyAsync(new Supplier () { @Override public Integer get() { return Integer.parseInt(s); } }); } }); f2.whenCompleteAsync(new BiConsumer () { @Override public void accept(Integer integer, Throwable throwable) { System.out.println(integer); } }); }
输出:
06-12 19:16:03.551 13095-13134/zhangphil.test I/System.out: 2018
转载地址:http://mmvta.baihongyu.com/