1.handle
public <U> CompletableFuture<U> handle( BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn);
public <U> CompletableFuture<U> handleAsync(BiFunction<? super T, Throwable, ? extends U> fn, Executor executor);
和 complete 一样,可对结果做最后的处理(可处理异常),可改变返回值
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(计算完成时回调方法)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 09:48:36
*/
public class CompletableFutureDemo2 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,100,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
/**
* 方法完成后的感知
*/
// CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
// System.out.println("当前线程:" + Thread.currentThread().getId());
// int i = 10 / 0;
// System.out.println("运行结果:" + i);
// return i;
// }, threadPoolExecutor).whenComplete((res,exception)->{
// //能感知异常,但不能修改返回数据
// System.out.println("异步任务成功完成啦....结果是:" + res + ";异常是:" + exception);
// }).exceptionally(throwable -> {
// //能感知异常,能修改返回数据(作为默认值)
// return 10;
// });
/**
* handle:方法执行完成后的处理
*/
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 0;
System.out.println("运行结果:" + i);
return i;
}, threadPoolExecutor).handle((res,throwable)->{
if(res != null){
return res * 10;
}
if(throwable != null){
return 0;
}
return 10;
});
Integer integer = integerCompletableFuture.get();
System.out.println("main......end......." + integer);
}
}
2.线程串行化方法
public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn);
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn);
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor);
public CompletableFuture<Void> thenAccept(Consumer<? super T> action);
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletableFuture<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor);
public CompletableFuture<Void> thenRun(Runnable action);
public CompletableFuture<Void> thenRunAsync(Runnable action);
public CompletableFuture<Void> thenRunAsync(Runnable action,Executor executor);
- thenApply 方法:当一个线程依赖另一个线程时,获取上一个任务返回的结果,并返回当前任务的返回值。
- thenAccept 方法:消费处理结果。接收任务的处理结果,并消费处理,无返回结果。
- thenRun 方法:只要上面的任务执行完成,就开始执行 thenRun,只是处理完任务后,执行thenRun 的后续操作
- 带有 Async 默认是异步执行的,以上都要前置任务成功完成
Function<? super T,? extends U> T:上一个任务返回结果的类型 U:当前任务的返回值类型
2.1.thenRunAsync
不能获取到上一步执行的结果,无返回值
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(线程串行化方法)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:00:36
*/
public class CompletableFutureDemo3 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
/**
* 线程串行化
* 1.thenRunXXX: 不能获取到上一步执行的结果,无返回值
*/
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}, threadPoolExecutor).thenRunAsync(() -> {
System.out.println("任务2启动.................");
}, threadPoolExecutor);
System.out.println("main......end......." );
}
}
2.2.thenAcceptAsync
能获取到上一步执行的结果,无返回值
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(线程串行化方法)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:00:36
*/
public class CompletableFutureDemo3 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
/**
* 线程串行化
* 2.thenAcceptXXX: 能获取到上一步执行的结果,无返回值
*/
CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}, threadPoolExecutor).thenAcceptAsync(res -> {
System.out.println("任务2启动..............." + res);
},threadPoolExecutor);
System.out.println("main......end......." );
}
}
2.3.thenApplyAsync
能获取到上一步执行的结果,有返回值
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(线程串行化方法)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:00:36
*/
public class CompletableFutureDemo3 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
/**
* 线程串行化
* 1.thenRunXXX: 不能获取到上一步执行的结果,无返回值
* 2.thenAcceptXXX: 能获取到上一步执行的结果,无返回值
* 3.thenApplyXXX: 能获取到上一步执行的结果,有返回值
*/
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}, threadPoolExecutor).thenApplyAsync(res -> {
System.out.println("任务2启动..............." + res);
return "java小生不才" + res;
}, threadPoolExecutor);
System.out.println("main......end......." + future.get());
}
}
3.两任务组合 - 都要完成
两个任务必须都完成,触发该任务
- thenCombine:组合两个 future,获取两个 future 的返回结果,并返回当前任务的返回值
- thenAcceptBoth:组合两个 future,获取两个 future 任务的返回结果,然后处理任务,没有返回值
- runAfterBoth:组合两个 future,不需要获取 future 的结果,只需两个 future 处理完任务后,处理该任务
3.1.runAfterBothAsync
组合两个future,不能感知前2个future的结果,只需两个future处理完任务后,执行处理第3个任务
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(两任务组合-2个任务都要完成才执行任务3)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:03:36
*/
public class CompletableFutureDemo4 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}, threadPoolExecutor);
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2运行结果:");
return "hello";
}, threadPoolExecutor);
//runAfterBothXXX:组合两个future,不能感知前2个future的结果,只需两个future处理完任务后,执行处理第3个任务
future1.runAfterBothAsync(future2,() -> {
System.out.println("任务3.........");
}, threadPoolExecutor);
}
}
3.2.thenAcceptBothAsync
组合两个 future,获取两个 future 任务的返回结果,然后处理任务,没有返回值
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(两任务组合-2个任务都要完成才执行任务3)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:03:36
*/
public class CompletableFutureDemo4 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}, threadPoolExecutor);
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2运行结果:");
return "hello";
}, threadPoolExecutor);
//1.runAfterBothXXX:组合两个future,不能感知前2个future的结果,只需两个future处理完任务后,执行处理第3个任务
// future1.runAfterBothAsync(future2,() -> {
// System.out.println("任务3.........");
// }, threadPoolExecutor);
//2.thenAcceptBothXXX:组合两个 future,获取两个 future 任务的返回结果,然后处理任务,没有返回值
future1.thenAcceptBothAsync(future2,(f1res,f2res)->{
System.out.println("任务3.........任务1结果: " + f1res + ",任务2结果: " + f2res);
},threadPoolExecutor);
System.out.println("main......end.......");
}
}
3.3.thenCombineAsync
组合两个future,获取两个future的返回结果,并返回当前任务的返回值
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(两任务组合-2个任务都要完成才执行任务3)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:03:36
*/
public class CompletableFutureDemo4 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}, threadPoolExecutor);
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
System.out.println("任务2运行结果:");
return "hello";
}, threadPoolExecutor);
//1.runAfterBothXXX:组合两个future,不能感知前2个future的结果,只需两个future处理完任务后,执行处理第3个任务
// future1.runAfterBothAsync(future2,() -> {
// System.out.println("任务3.........");
// }, threadPoolExecutor);
//2.thenAcceptBothXXX:组合两个 future,获取两个 future 任务的返回结果,然后处理任务,没有返回值
// future1.thenAcceptBothAsync(future2,(f1res,f2res)->{
// System.out.println("任务3.........任务1结果: " + f1res + ",任务2结果: " + f2res);
// },threadPoolExecutor);
//3.thenCombineXXX: 组合两个future,获取两个future的返回结果,并返回当前任务的返回值
CompletableFuture<String> future3 = future1.thenCombineAsync(future2, (f1res, f2res) -> {
System.out.println("任务3.........任务1结果: " + f1res + ",任务2结果: " + f2res);
return "任务3可以有返回值";
}, threadPoolExecutor);
System.out.println("main......end......." + future3.get());
}
}
4.两任务组合—一个完成
两任务组合------只要有一个完成就执行任务3
4.1.runAfterEitherAsync
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(两任务组合------只要有一个完成就执行任务3)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:06:36
*/
public class CompletableFutureDemo5 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}, threadPoolExecutor);
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
try {
Thread.sleep(5000);
System.out.println("任务2运行结果:");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}, threadPoolExecutor);
//1.runAfterEitherXXX:组合两个future,不能感知前2个future的结果,只需1个future处理完任务后,执行处理第3个任务
future1.runAfterEitherAsync(future2,()->{
System.out.println("任务3............");
},threadPoolExecutor);
System.out.println("main......end......." );
}
}
4.2.acceptEitherAsync
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(两任务组合------只要有一个完成就执行任务3)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:06:36
*/
public class CompletableFutureDemo5 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}, threadPoolExecutor);
CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
try {
Thread.sleep(5000);
System.out.println("任务2运行结果:");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}, threadPoolExecutor);
//1.runAfterEitherXXX:组合两个future,不能感知前2个future的结果,只需1个future处理完任务后,执行处理第3个任务
// future1.runAfterEitherAsync(future2,()->{
// System.out.println("任务3............");
// },threadPoolExecutor);
//2.acceptEitherXXX:组合两个future,感知之前任务的结果,但没有返回值
future1.acceptEitherAsync(future2,(res)->{
System.out.println("任务3.........之前任务结果: " + res );
},threadPoolExecutor);
System.out.println("main......end......." );
}
}
4.3.applyToEitherAsync
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(两任务组合------只要有一个完成就执行任务3)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:06:36
*/
public class CompletableFutureDemo5 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务1线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("任务1运行结果:" + i);
return i;
}, threadPoolExecutor);
CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("任务2线程:" + Thread.currentThread().getId());
try {
Thread.sleep(5000);
System.out.println("任务2运行结果:");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}, threadPoolExecutor);
//1.runAfterEitherXXX:组合两个future,不能感知前2个future的结果,只需1个future处理完任务后,执行处理第3个任务
// future1.runAfterEitherAsync(future2,()->{
// System.out.println("任务3............");
// },threadPoolExecutor);
//2.acceptEitherXXX:组合两个future,感知之前任务的结果,但没有返回值
// future1.acceptEitherAsync(future2,(res)->{
// System.out.println("任务3.........之前任务结果: " + res );
// },threadPoolExecutor);
//3.applyToEitherXXX: 感知之前任务的结果,有返回值
CompletableFuture<String> future3 = future1.applyToEitherAsync(future2, (res) -> {
System.out.println("任务3.........任务1结果: " + res);
return "任务3可以有返回值";
}, threadPoolExecutor);
System.out.println("main......end......." + future3.get() );
}
}
5.多任务组合
package com.atguigu.gulimall.search.thread;
import java.util.concurrent.*;
/**
* 异步编排CompletableFuture(多任务组合)
* @author zfh
* @email hst1406959716@163.com
* @date 2022-01-04 10:07:56
*/
public class CompletableFutureDemo6 {
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,20,10,
TimeUnit.SECONDS,new LinkedBlockingDeque<>(1000),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("main......start.....");
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品图片.....");
return "iphone13.jpg";
}, threadPoolExecutor);
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("查询商品属性.....");
return "远峰蓝 256G";
}, threadPoolExecutor);
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("查询商品介绍.....");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Iphone13 PRO MAX";
}, threadPoolExecutor);
//allOf 等待所有任务完成
// CompletableFuture<Void> future = CompletableFuture.allOf(future1, future2, future3);
//anyOf 只要有一个任务完成
CompletableFuture<Object> future = CompletableFuture.anyOf(future1, future2, future3);
System.out.println("main......end......." );
}
}