1. future 多个whenComplete , 多个whenComplete会执行, 相互间没有影响
@Test
public void futureTest(){
CompletableFuture future = new CompletableFuture();
future.whenComplete((r, t)->{
logger.info("when 1, r:{}, t:{}", r, t);
});
future.whenComplete((r, t)->{
logger.info("when 2, r:{}, t:{}", r, t);
// throw new RuntimeException("1234566");
}).whenComplete((r, t)->{
logger.info("when 2+1, r:{}, t:{}", r, t);
}).thenApply(r ->{
logger.info("apply run :{}", r);
return r;
}).whenComplete((r, t)->{
logger.info("when 2+2, r:{}, t:{}", r, t);
});
future.whenComplete((r, t)->{
logger.info("when 3, r:{}, t:{}", r, t);
});
future.complete(123);
}
[INFO ] 10:25:21.446[main]c.e.s.t.utils.TestUtils.lambda$futureTest$7:311 -when 3, r:123, t:null
[INFO ] 10:25:21.449[main]c.e.s.t.utils.TestUtils.lambda$futureTest$3:299 -when 2, r:123, t:null
[INFO ] 10:25:21.450[main]c.e.s.t.utils.TestUtils.lambda$futureTest$4:302 -when 2+1, r:123, t:null
[INFO ] 10:25:21.450[main]c.e.s.t.utils.TestUtils.lambda$futureTest$5:304 -apply run :123
[INFO ] 10:25:21.450[main]c.e.s.t.utils.TestUtils.lambda$futureTest$6:307 -when 2+2, r:123, t:null
[INFO ] 10:25:21.450[main]c.e.s.t.utils.TestUtils.lambda$futureTest$2:296 -when 1, r:123, t:null
Disconnected from the target VM, address: '127.0.0.1:10833', transport: 'socket'
2. future.whenComplete 抛出异常, 不同whenComplete之间无影响
@Test
public void futureTest(){
CompletableFuture future = new CompletableFuture();
future.whenComplete((r, t)->{
logger.info("when 1, r:{}, t:{}", r, t);
});
future.whenComplete((r, t)->{
logger.info("when 2, r:{}, t:{}", r, t);
throw new RuntimeException("1234566");
}).whenComplete((r, t)->{
logger.info("when 2+1, r:{}, t:{}", r, t);
}).thenApply(r ->{
logger.info("apply run :{}", r);
return r;
}).whenComplete((r, t)->{
logger.info("when 2+2, r:{}, t:{}", r, t);
});
future.whenComplete((r, t)->{
logger.info("when 3, r:{}, t:{}", r, t);
});
future.complete(123);
}
[INFO ] 10:26:01.400[main]c.e.s.t.utils.TestUtils.lambda$futureTest$7:311 -when 3, r:123, t:null
[INFO ] 10:26:01.405[main]c.e.s.t.utils.TestUtils.lambda$futureTest$3:299 -when 2, r:123, t:null
[INFO ] 10:26:01.408[main]c.e.s.t.utils.TestUtils.lambda$futureTest$4:302 -when 2+1, r:null, t:{}
java.util.concurrent.CompletionException: java.lang.RuntimeException: 1234566
[INFO ] 10:26:01.408[main]c.e.s.t.utils.TestUtils.lambda$futureTest$6:307 -when 2+2, r:null, t:{}
java.util.concurrent.CompletionException: java.lang.RuntimeException: 1234566
[INFO ] 10:26:01.409[main]c.e.s.t.utils.TestUtils.lambda$futureTest$2:296 -when 1, r:123, t:null
Disconnected from the target VM, address: '127.0.0.1:10870', transport: 'socket'