1、计算完成时回调
1.1、runAsync()无返回值
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Void> runAsync = CompletableFuture.runAsync((() -> {
System.out.println("线程池执行了");
}), executor);
System.out.println("main结束");
}
1.2、supplyAsync()有返回值
//方法完成后的感知
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync((() -> {
return 1;
}), executor).whenCompleteAsync((res,excption)->{
//异步任务完成时需要做的事情,可以获取异常信息但是无法修改返回数据
//(res,excption) res异步任务执行完的结果,excption异步任务执行的异常
}).exceptionally(throwable -> {
//出现异常时处理逻辑,可以返回默认值
return 1;
});
System.out.println(supplyAsync.get());
System.out.println("main结束");
}
2、handle()方法
//方法执行完成后的处理
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync((() -> {
return 1;
}), executor).handle((res,excption)->{
if (res != null){
//方法正常执行完成
}
if (excption != null){
//方法执行时出现异常的处理
}
return 1;
});
System.out.println(supplyAsync.get());
System.out.println("main结束");
}
3、线程串行化
3.1、thenRunAsync()无法获取上一步执行结果
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture.supplyAsync((() -> {
System.out.println("线程1开始了");
return 1;
}), executor).thenRunAsync(()->{
System.out.println("线程2开始了");
});
System.out.println("main结束");
}
3.2、supplyAsync()可以感知上一步执行结果,但无返回值
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture.supplyAsync((() -> {
System.out.println("线程1开始了");
return 1;
}), executor).thenAcceptAsync(res ->{
System.out.println("上一步执行结果:"+res);
});
System.out.println("main结束");
}
3.3、thenApplyAsync() 感知上一步结果,并返回处理完成的值
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Integer> thenApplyAsync = CompletableFuture.supplyAsync((() -> {
System.out.println("线程1开始了");
return 1;
}), executor).thenApplyAsync(res -> {
System.out.println("上一步执行结果:" + res);
return 2 + res;
}, executor);
System.out.println(thenApplyAsync.get());
System.out.println("main结束");
}
4、两任务组合(都要完成)
4.1、runAfterBothAsync() 不能获取前面线程的执行结果且无返回值
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程1开始了");
return 1;
}), executor);
CompletableFuture<String > future2 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程2开始了");
return "hello";
}), executor);
future1.runAfterBothAsync(future2,()->{
System.out.println("线程3执行,不能获取前面线程的执行结果且无返回结果");
},executor);
System.out.println("main结束");
}
4.2、thenAcceptBothAsync()可以获取前面线程的值,但方法本身无返回值
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程1开始了");
return 1;
}), executor);
CompletableFuture<String > future2 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程2开始了");
return "hello";
}), executor);
future1.thenAcceptBothAsync(future2,(f1,f2)->{
System.out.println("任务3 开始,前面线程的结果为:"+f1+" "+f2);
},executor);
System.out.println("main结束");
}
4.3、thenCombineAsync() 可以获取前面线程的值,同时可以将结果返回
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程1开始了");
return 1;
}), executor);
CompletableFuture<String > future2 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程2开始了");
return "hello";
}), executor);
CompletableFuture<String> combineAsync = future1.thenCombineAsync(future2, (f1, f2) -> {
return f1 + "" + f2;
}, executor);
System.out.println(combineAsync.get());
System.out.println("main结束");
}
5、两任务组合(一个完成)
5.1、runAfterEitherAsync() 不能获取前面线程的值,也没有返回值
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程1开始了");
return 1;
}), executor);
CompletableFuture<String > future2 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程2开始了");
return "hello";
}), executor);
future1.runAfterEitherAsync(future2,()->{
System.out.println("线程3开始");
},executor);
System.out.println("main结束");
}
5.2、acceptEitherAsync() 可以获取上一个线程的结果,但无返回值
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程1开始了");
return 1;
}), executor);
CompletableFuture<Object > future2 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程2开始了");
return "hello";
}), executor);
future1.acceptEitherAsync(future2,(res)->{
System.out.println("线程3开始,上一个线程执行的结果:"+res);
},executor);
System.out.println("main结束");
}
5.3、applyToEitherAsync() 可以获取上一个线程的结果,同时可以返回结果
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程1开始了");
return 1;
}), executor);
CompletableFuture<Object > future2 = CompletableFuture.supplyAsync((() -> {
System.out.println("线程2开始了");
return "hello";
}), executor);
CompletableFuture<String> eitherAsync = future1.applyToEitherAsync(future2, (res) -> {
System.out.println("线程3开始");
System.out.println("上一个线程的结果" + res);
return "线程3" + res;
}, executor);
System.out.println(eitherAsync.get());
System.out.println("main结束");
}
6、多任务组合
6.1、allOf() 等待所有结果完成,再执行
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Object> futureImg = CompletableFuture.supplyAsync((() -> {
System.out.println("查询商品图片");
return "hello.jpg";
}), executor);
CompletableFuture<Object> futureAttr = CompletableFuture.supplyAsync((() -> {
System.out.println("查询商品属性");
return "黑色 + 25G";
}), executor);
CompletableFuture<Object> futureDesc = CompletableFuture.supplyAsync((() -> {
System.out.println("查询商品介绍");
return "华为";
}), executor);
CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
allOf.get();//等待所有结果完成
System.out.println("main结束,结果是:"+futureImg.get()+futureAttr.get()+futureDesc.get());
}
6.2、anyOf()只要有一个线程执行完成,就执行下面的语句
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行");
CompletableFuture<Object> futureImg = CompletableFuture.supplyAsync((() -> {
System.out.println("查询商品图片");
return "hello.jpg";
}), executor);
CompletableFuture<Object> futureAttr = CompletableFuture.supplyAsync((() -> {
System.out.println("查询商品属性");
return "黑色 + 25G";
}), executor);
CompletableFuture<Object> futureDesc = CompletableFuture.supplyAsync((() -> {
System.out.println("查询商品介绍");
return "华为";
}), executor);
CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
anyOf.get(); //只要有一个线程完成了,就执行
System.out.println("main结束,结果是:"+anyOf.get());
}
7、异步调用(多线程)下数据共享
//创建线程池并设置大小
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main执行,线程ID:"+ Thread.currentThread().getId());
//获取主线程的请求数据
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
CompletableFuture<Object> futureImg = CompletableFuture.supplyAsync((() -> {
//每一个线程都来共享主线程的请求数据
RequestContextHolder.setRequestAttributes(requestAttributes);
System.out.println("异步线程,线程ID:"+ Thread.currentThread().getId());
System.out.println("查询商品图片");
return "hello.jpg";
}), executor);
CompletableFuture<Object> futureAttr = CompletableFuture.supplyAsync((() -> {
//每一个线程都来共享主线程的请求数据
RequestContextHolder.setRequestAttributes(requestAttributes);
System.out.println("异步线程,线程ID:"+ Thread.currentThread().getId());
System.out.println("查询商品属性");
return "黑色 + 25G";
}), executor);
CompletableFuture<Object> futureDesc = CompletableFuture.supplyAsync((() -> {
//每一个线程都来共享主线程的请求数据
RequestContextHolder.setRequestAttributes(requestAttributes);
System.out.println("异步线程,线程ID:"+ Thread.currentThread().getId());
System.out.println("查询商品介绍");
return "华为";
}), executor);
CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
anyOf.get(); //只要有一个线程完成了,就执行
System.out.println("main结束,结果是:"+anyOf.get());
}