CompletableFuture提供了四个静态方法创建异步任务:
CompletableFuture.runAsync(Runnable runnable);
CompletableFuture.runAsync(Runnable runnable,Executor executor);
CompletableFuture.supplyAsync(Supplier<U> supplier);
CompletableFuture.supplyAsync(Supplier<U> supplier,Executor executor);
其中runXXX没有返回结果,supplyXXX可以获取返回结果;
都可以传入自定义的线程池,否则使用默认的线程池;
whenComplete获取上任务的结果:
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).whenComplete((result,exception)->{
System.out.println("异步任务完成了,结果:"+result);
System.out.println("异步任务异常:"+exception);
});
}
// 执行结果:
-----main start--------1
当前线程12------当前结果5
异步任务完成了,结果:5
异步任务异常:null
whenComplete获取上任务抛出的异常信息:
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int a = 10 / 0;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).whenComplete((result,exception)->{
System.out.println("异步任务完成了,结果:"+result);
System.out.println("异步任务异常:"+exception);
});
}
// 执行结果:
-----main start--------1
异步任务完成了,结果:null
异步任务异常:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
exceptionally捕获异常信息,返回默认值
public static void main(String[] args) throws Exception {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int a = 10 / 0;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).whenComplete((result,exception)->{
System.out.println("异步任务完成了,结果:"+result);
System.out.println("异步任务异常:"+exception);
}).exceptionally((exception)->{
System.out.println("捕获异步任务异常:"+exception);
return 10;
});
System.out.println("任务结果:"+future.get());
}
// 执行结果:
-----main start--------1
异步任务完成了,结果:null
异步任务异常:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
捕获异步任务异常:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
任务结果:10
exceptionally可以捕获到whenComplete的异常
public static void main(String[] args) throws Exception {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).whenComplete((result,exception)->{
System.out.println("异步任务完成了,结果:"+result);
System.out.println("异步任务异常:"+exception);
throw new RuntimeException("whenComplete抛出异常");
}).exceptionally((exception)->{
System.out.println("捕获异步任务异常:"+exception);
return 10;
});
System.out.println("任务结果:"+future.get());
}
// 执行结果:
-----main start--------1
当前线程12------当前结果5
异步任务完成了,结果:5
异步任务异常:null
捕获异步任务异常:java.util.concurrent.CompletionException: java.lang.RuntimeException: whenComplete抛出异常
任务结果:10
exceptionally抛出异常,任务结束
public static void main(String[] args) throws Exception {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).whenComplete((result,exception)->{
System.out.println("异步任务完成了,结果:"+result);
System.out.println("异步任务异常:"+exception);
throw new RuntimeException("whenComplete抛出异常");
}).exceptionally((exception)->{
System.out.println("捕获异步任务异常:"+exception);
throw new RuntimeException("任务执行失败");
});
System.out.println("任务结果:"+future.get());
}
// 执行结果:
-----main start--------1
当前线程12------当前结果5
异步任务完成了,结果:5
异步任务异常:null
捕获异步任务异常:java.util.concurrent.CompletionException: java.lang.RuntimeException: whenComplete抛出异常
Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.RuntimeException: 任务执行失败
whenComplete 和 whenCompleteAsync 区别:
handle捕获异常,返回默认值
public static void main(String[] args) throws Exception {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int a = 10 / 0;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).handle((result,exception)->{
System.out.println("异步任务完成了,结果:"+result);
System.out.println("异步任务异常:"+exception);
return 20;
});
System.out.println("任务结果:"+future.get());
}
//执行结果:
-----main start--------1
异步任务完成了,结果:null
异步任务异常:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
任务结果:20
handle抛出异常,任务结束
public static void main(String[] args) throws Exception {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int a = 10 / 0;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).handle((result,exception)->{
System.out.println("异步任务完成了,结果:"+result);
System.out.println("异步任务异常:"+exception);
throw new RuntimeException("handle抛出异常");
});
System.out.println("任务结果:"+future.get());
}
//执行结果:
-----main start--------1
异步任务完成了,结果:null
异步任务异常:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.RuntimeException: handle抛出异常
线程串行化thenRun():不能获取到上一步任务的执行结果,不能捕获上一步异常,上一步异常任务结束
public static void main(String[] args) throws Exception {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture.supplyAsync(() -> {
int a = 10 / 0;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).thenRun(()->{
System.out.println("任务2启动了");
});
System.out.println("任务结果:");
}
// 执行结果:
-----main start--------1
任务结果:
线程串行化thenRun():不能获取到上一步任务的执行结果,无返回值
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).thenRun(()->{
System.out.println("任务2启动了");
});
System.out.println("任务结果:");
}
// 执行结果:
-----main start--------1
当前线程12------当前结果5
任务2启动了
任务结果:
线程串行化thenRun():不能获取到上一步任务的执行结果,无返回值,发生异常时不会抛出异常
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).thenRun(()->{
System.out.println("任务2启动了");
throw new RuntimeException("任务2失败了");
});
System.out.println("任务结果:");
}
// 执行结果:
-----main start--------1
当前线程12------当前结果5
任务2启动了
任务结果:
线程串行化thenAccept():可以获取上一步结果,异常与thenRun()方法一致
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).thenAccept((result)->{
System.out.println("任务1执行结果"+result);
System.out.println("任务2启动了");
throw new RuntimeException("任务2失败了");
});
System.out.println("任务结果:");
}
//执行结果:
-----main start--------1
当前线程12------当前结果5
任务1执行结果5
任务2启动了
任务结果:
线程串行化thenAccept():可以获取上一步结果,异常与thenRun()方法一致
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程"+Thread.currentThread().getId()+"------当前结果" + a);
return a;
}).thenAccept((result)->{
System.out.println("任务1执行结果"+result);
System.out.println("任务2启动了");
throw new RuntimeException("任务2失败了");
});
System.out.println("任务结果:");
}
//执行结果:
-----main start--------1
当前线程12------当前结果5
任务1执行结果5
任务2启动了
任务结果:
线程串行化thenApply():可以获取上一步结果,有返回值
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程" + Thread.currentThread().getId() + "------当前结果" + a);
return a;
}).thenApply((result) -> {
System.out.println("任务1执行结果" + result);
System.out.println("任务2启动了");
return 100;
});
Integer result=null;
try {
result = future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("任务结果:"+result);
}
//执行结果:
-----main start--------1
当前线程12------当前结果5
任务1执行结果5
任务2启动了
任务结果:100
线程串行化thenApply():可以获取上一步结果,有返回值,可抛出异常,上一步发生异常任务结束
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程" + Thread.currentThread().getId() + "------当前结果" + a);
return a;
}).thenApply((result) -> {
System.out.println("任务1执行结果" + result);
System.out.println("任务2启动了");
throw new RuntimeException("任务2发生异常了");
});
Integer result=null;
try {
result = future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("任务结果:"+result);
}
//执行结果:
-----main start--------1
当前线程12------当前结果5
任务1执行结果5
任务2启动了
任务结果:null
java.util.concurrent.ExecutionException: java.lang.RuntimeException: 任务2发生异常了
thenApply 、thenAccept、thenRun 区别:
runAfterBoth()无返回结果:任务1和任务2执行完成,执行当前任务;任务1和任务2任一抛出异常,任务3不执行;任一任务不会抛出异常;
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程" + Thread.currentThread().getId() + "------任务一结果:" + a);
return a;
});
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
int a = 10 / 0;
System.out.println("当前线程" + Thread.currentThread().getId() + "------任务二结果:" + a);
return a;
});
future1.runAfterBoth(future2,()->{
int a=1/0;
System.out.println("任务三开始了"+a);
});
System.out.println("任务结果:");
}
//执行结果:
-----main start--------1
当前线程12------任务一结果:5
任务结果:
runAfterBoth()无返回结果,可得到任务1和任务2结果:任务1和任务2任一抛出异常,任务3不执行;任一任务不会抛出异常;
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程" + Thread.currentThread().getId() + "------任务一结果:" + a);
return a;
});
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程" + Thread.currentThread().getId() + "------任务二结果:" + a);
return a;
});
future1.thenAcceptBothAsync(future2,(f1,f2)->{
System.out.println("任务一结果:"+f1+"---任务二结果:"+f2);
int a=1/0;
System.out.println("任务三开始了"+a);
});
System.out.println("任务结果:");
}
-----main start--------1
当前线程12------任务一结果:5
当前线程12------任务二结果:5
任务结果:
任务一结果:5---任务二结果:5
runAfterBoth()有返回结果,可得到任务1和任务2结果:任务1和任务2任一抛出异常,任务3不执行;可捕获任务3的异常;
public static void main(String[] args) {
System.out.println("-----main start--------"+Thread.currentThread().getId());
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程" + Thread.currentThread().getId() + "------任务一结果:" + a);
return a;
});
CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
int a = 10 / 2;
System.out.println("当前线程" + Thread.currentThread().getId() + "------任务二结果:" + a);
return a;
});
CompletableFuture<Integer> future = future1.thenCombineAsync(future2, (f1, f2) -> {
System.out.println("任务一结果:" + f1 + "---任务二结果:" + f2);
int a = 1 / 0;
System.out.println("任务三开始了" + a);
return a;
});
Integer integer = null;
try {
integer = future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("任务结果:"+integer);
}
-----main start--------1
当前线程12------任务一结果:5
当前线程12------任务二结果:5
任务一结果:5---任务二结果:5
任务结果:null
java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero
thenCombine 、thenAcceptBoth、runAfterBoth 区别:
applyToEither 、acceptEither、runAfterEither 区别:
allOf 、anyOf 区别:
原文:https://www.cnblogs.com/kevin-ying/p/14383380.html