Java CompletableFuture异步线程联合执行thenCombine(6)
private void method() throws ExecutionException, InterruptedException {
//第一个任务。
CompletableFuture<String> f1 = CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "zhang";
}
});
//第二个任务把第一个任务联合起来。
CompletableFuture<String> f2 = f1.thenCombine(
CompletableFuture.supplyAsync(new Supplier<String>() {
@Override
public String get() {
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "phil";
}
}), new BiFunction<String, String, String>() {
@Override
public String apply(String s1, String s2) {
return s1 + s2;
}
}
);
System.out.println("等待联合任务的全部执行完毕...");
f2.whenCompleteAsync(new BiConsumer<String, Throwable>() {
@Override
public void accept(String s, Throwable throwable) {
System.out.println("联合任务均完成");
System.out.println(s);
}
});
System.out.println("代码运行至此。");
}
输出:
06-14 16:37:41.113 21703-21703/zhangphil.test I/System.out: 等待联合任务的全部执行完毕...
06-14 16:37:41.114 21703-21703/zhangphil.test I/System.out: 代码运行至此。
06-14 16:37:44.114 21703-21757/zhangphil.test I/System.out: 联合任务均完成