CompletableFuture的thenCombineAsync
代码:
private void test() {System.out.println("开始...");CompletableFuture.supplyAsync(new Supplier() {@Override
public String get() {try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("返回 zhang");return "zhang";}}).thenCombineAsync(CompletableFuture.supplyAsync(new Supplier() {@Override
public String get() {try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("返回 phil");return "phil";}}), new BiFunction() {@Override
public Object apply(String s1, String s2) {String s = s1 + " , " + s2;System.out.println("apply:" + s);return s;}}).whenCompleteAsync(new BiConsumer() {@Override
public void accept(Object o, Throwable throwable) {System.out.println("accept:" + o.toString());}});System.out.println("运行至此.");
}
运行输出:
07-10 16:12:37.230 29410-29410/zhangphil.test I/System.out: 开始...
07-10 16:12:37.235 29410-29410/zhangphil.test I/System.out: 运行至此.
07-10 16:12:38.234 29410-29450/zhangphil.test I/System.out: 返回 zhang
07-10 16:12:40.236 29410-29451/zhangphil.test I/System.out: 返回 phil
07-10 16:12:40.237 29410-29451/zhangphil.test I/System.out: apply:zhang , phil
07-10 16:12:40.237 29410-29450/zhangphil.test I/System.out: accept:zhang , phil