1、runAsync(Runnable) 异步方法,默认线程池,无返回值
2、runAsync(Runnable,Executor) 异步方法,自定义线程池,无返回值
public static void main(String[] args) {
System.out.println("test start");
CompletableFuture.runAsync(() -> { System.out.println("test runAsync");});
ExecutorService executors = Executors.newFixedThreadPool(1);
CompletableFuture.runAsync(() -> { System.out.println("test runAsync with executors");},executors);
}
输出结果:
test start
test runAsync
test runAsync with executors
3、runAsync(Runnable) 异步方法,默认线程池,有返回值
4、runAsync(Runnable,Executor) 异步方法,自定义线程池,有返回值
public static void main(String[] args) {
System.out.println("test start");
CompletableFuture<String> test1 = CompletableFuture.supplyAsync(() -> { System.out.println("test supplyAsync");return "zhao";});
System.out.println(test1.join());
ExecutorService executors = Executors.newFixedThreadPool(1);
CompletableFuture<String> test2 = CompletableFuture.supplyAsync(() -> { System.out.println("test supplyAsync with executors");return "lao tuo";},executors);
System.out.println(test2.join());
}
输出结果:
test start
test supplyAsync
zhao
test supplyAsync with executors
lao tuo
5、join,get
都是获取异步线程执行结果,例如上面例子;阻塞线程等待异步结果
但是get抛出来检查异常
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("test start");
CompletableFuture<String> test1 = CompletableFuture.supplyAsync(() -> { System.out.println("test supplyAsync");return "zhao";});
System.out.println(test1.join());
ExecutorService executors = Executors.newFixedThreadPool(1);
CompletableFuture<String> test2 = CompletableFuture.supplyAsync(() -> { System.out.println("test supplyAsync with executors");return "lao tuo";},executors);
System.out.println(test2.get());
}
6、thenApply方法
使用thenApply来将两个线程串行化,如果任务出现异常不会执行thenApply
public static void main(String[] args) {
System.out.println("test start");
CompletableFuture<String> test1 = CompletableFuture.supplyAsync(() -> {
System.out.println("test supplyAsync");
return "zhao";
}).thenApply(then ->{
return then+" lao tuo";
});
System.out.println(test1.join());
}
输出结果:
test start
test supplyAsync
zhao lao tuo
如果thenApply 执行线程需要异步化,使用thenApplyAsync
public static void main(String[] args) {
System.out.println("test start");
CompletableFuture<String> test1 = CompletableFuture.supplyAsync(() -> {
System.out.println("test supplyAsync");
return "zhao";
}).thenApplyAsync(then ->{
return then+" lao tuo";
});
System.out.println(test1.join()); }
7、handle方法
执行任务完成后对结果处理,可以根据任务是否异常来做处理
public static void main(String[] args) {
System.out.println("test start");
CompletableFuture<String> test1 = CompletableFuture.supplyAsync(() -> {
System.out.println("test supplyAsync");
int a = 1/0;
return "zhao";
}).handle((t,throwable) ->{
if(throwable != null){
System.out.println(throwable);
}else {
t = t + " lao tuo";
}
return t;
});
System.out.println(test1.join());
}
输出结果:
test start
test supplyAsync
java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
null
8、thenCombine方法
会将两个线程执行万,调用者线程会将两个异步任务的结果一块处理
public static void main(String[] args) {
System.out.println("test start");
CompletableFuture<String> test1 = CompletableFuture.supplyAsync(() -> {
System.out.println("test supplyAsync");
return "zhao";
});
CompletableFuture<String> test2 = CompletableFuture.supplyAsync(() -> {
System.out.println("test supplyAsync");
return " lao tuo ";
});
CompletableFuture<String> result = test1.thenCombine(test2,(result1,result2) ->{
return result1+result2;
});
System.out.println(result.join());
}
输出结果:
test start
test supplyAsync
test supplyAsync
zhao lao tuo
如果想要异步执行使用方法thenCombineAsync
9、thenCombine方法
第一个任务完成之后,将第一个任务结果作为参数传给第二个任务
public static void main(String[] args) {
System.out.println("test start");
CompletableFuture<String> test1 = CompletableFuture.supplyAsync(() -> {
System.out.println("test supplyAsync");
return "zhao";
}).thenCompose(result1 ->{
return CompletableFuture.supplyAsync(() -> {
System.out.println("test supplyAsync");
return result1 + " lao tuo";
});
});
System.out.println(test1.join());
}
输出结果:
test start
test supplyAsync
test supplyAsync
zhao lao tuo