public static void main1(String[] args) throws ExecutionException, InterruptedException {
System.out.println("开始");
//异步编排1 无返回值runAsync()
CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("结果运行:" + i);
}, executor);
//异步编排2 有返回值 supplyAsync()
//方法完成后的感知
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 0;
System.out.println("结果运行:" + i);
return i;
}, executor).whenComplete((result, exception)->{
//虽然能得到异常信息,却不能修改返回数据,类似监听器
System.out.println("异步任务完成了,结果是:" + result + ",异常是:" + exception);
}).exceptionally((throwable -> {
//可以感知异常,同时返回默认数据
return 10;
}));
System.out.println(future2.get());
//handle()方法执行后的处理(无论成功完成还是失败完成) 就算有异常 也想要结果
CompletableFuture<Integer> future2_1 = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 4;
System.out.println("结果运行:" + i);
return i;
}, executor).handle((result, throwable)->{
if (result != null) {
return result * 2;
}
if (throwable != null) {
//出现了异常
return 0;
}
return 0;
});
System.out.println(future2_1.get());
/**
* 串行化 A任务完成后 -> B任务执行
* 带Async的意思是:再开一个线程; 否则和A线程共用一个线程
*/
//thenRunAsync() 不能获取到上一步的执行结果 无返回值
CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 4;
System.out.println("结果运行:" + i);
return i;
}, executor).thenRunAsync(() -> {
System.out.println("任务2启动了");
}, executor);
//thenAcceptAsync() 能接受上一个任务的结果,但是无返回值
CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 4;
System.out.println("结果运行:" + i);
return i;
}, executor).thenAcceptAsync((result)->{
System.out.println("任务2启动了" + "上一步执行的结果是:" + result);
}, executor);
//thenAcceptAsync() 能接受上一个任务的结果,有返回值
CompletableFuture<String> future2_2 = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 4;
System.out.println("结果运行:" + i);
return i;
}, executor).thenApplyAsync((result) -> {
System.out.println("任务2启动了" + "上一步执行的结果是:" + result);
return "hello" + result;
}, executor);
System.out.println(future2_2.get());
/**
* 两个任务组合
* 两个任务都完成 然后执行第三个 A + B -> C
*/
CompletableFuture<Object> future3_1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1开始。当前线程:" + Thread.currentThread().getId());
int i = 10 / 4;
System.out.println("任务1结束。结果运行:" + i);
return i;
}, executor);
CompletableFuture<Object> future3_2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2开始。当前线程:" + Thread.currentThread().getId());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("任务2结束...");
return "hello";
}, executor);
//runAfterBothAsync()不能感知前两步的执行结果 自己也没有返回值
future3_1.runAfterBothAsync(future3_2, ()->{
System.out.println("任务3开始。");
}, executor);
//thenAcceptBothAsync()能感知前两步的执行结果 自己没有返回值
future3_1.thenAcceptBothAsync(future3_2, (f1, f2)->{
System.out.println("任务3开始。之前的结果:f1=" + f1 + ";f2=" + f2);
}, executor);
//thenCombineAsync()能感知前两步的执行结果, 还能处理前面两个任务的返回值,并生成返回值 自己有返回值
CompletableFuture<String> future3_3 = future3_1.thenCombineAsync(future3_2, (f1, f2) -> {
return f1 + ": " + f2 + "->ww";
}, executor);
System.out.println(future3_3.get());
/**
* 两个任务 只要有一个完成就行 就能执行第三个任务 A || B = C
*/
//runAfterEitherAsync() 不感知前面任务的结果,自己也没有返回值
future3_1.runAfterEitherAsync(future3_2, () -> {
System.out.println("任务3开始执行。");
}, executor);
//acceptEitherAsync() 能感知前面任务的结果,自己没有返回值
future3_1.acceptEitherAsync(future3_2, (result) -> {//要求任务1、2的返回类型必须相同
System.out.println("任务3开始执行。" + result);
}, executor);
//applyToEitherAsync() 能感知前面任务的结果,自己有返回值
CompletableFuture<String> future3_4 = future3_1.applyToEitherAsync(future3_2, (result) -> {//要求任务1、2的返回类型必须相同
System.out.println("任务3开始执行。" + result);
return result.toString() + "哈哈";
}, executor);
System.out.println(future3_4.get());
/**
* 多任务组合
*/
CompletableFuture<String> future4_1 = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的图片信息……");
return "hello.jpg";
}, executor);
CompletableFuture<String> future4_2 = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品的属性……");
return "黑色 2+64G";
}, executor);
CompletableFuture<String> future4_3 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("查询商品的介绍……");//模拟业务时间超长
} catch (InterruptedException e) {
e.printStackTrace();
}
return "apple";
}, executor);
//allOf() 所有任务结束后才能继续执行
CompletableFuture<Void> future4_4 = CompletableFuture.allOf(future4_1, future4_2, future4_3);
future4_4.get();//等待上面3个任务完成 不可以打印
System.out.println(future4_1.get() + future4_2.get() + future4_3.get());
//anyOf() 只要一个任务,结束就可以继续执行
CompletableFuture<Object> future4_5 = CompletableFuture.anyOf(future4_1, future4_2, future4_3);
future4_5.get();//等待所有任务完成 可以打印
System.out.println(future4_5.get());
System.out.println("结束");
}