一、CompletableFuture
1 异步编排,可以编配任务执行,与vue的promise类似。
futrue可以获取异步任务的结果
1 public static CompletableFuture<Void> runAsync(Runnable runnable) {
return asyncRunStage(asyncPool, runnable);
}
2 public static CompletableFuture<Void> runAsync(Runnable runnable,
Executor executor) {
return asyncRunStage(screenExecutor(executor), runnable);
}
3 public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) {
return asyncSupplyStage(asyncPool, supplier);
}
4 public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier,
Executor executor) {
return asyncSupplyStage(screenExecutor(executor), supplier);
}
结论:
runAsync 执行没有返回结果
supplyAsync 可以获取返货结果
可以传入自定义线程池,否则就用默认的线程池。
2 计算完成时回调方法
whencomplete(BitConsumer)
whencompleteAsync(BitConsumer)
whencompleteAsync(BitConsumer,Executor)
exceptionally(Function)
whencomplete 可以处理正常和异常结果
exceptionally处理异常情况
whencomplete 是执行当前任务的线程继续执行whencomplete的任务
whencompleteAsync 是执行whencompleteAsync这个任务继续提交线程池来执行。
方法不是以async结尾,意味着Action使用相同线程执行,而
Async可能使用其他线程执行(如果使用相同的线程池也可能被同一个线程选中执行)
@Test
void test1() {
System.out.println("main**********start");
Executor pool = getPool();
CompletableFuture.supplyAsync(()->{
System.out.println("当前线程**********"+Thread.currentThread().getId());
int num = 10/2;
System.out.println("结果**********"+num);
return num;
},pool).whenComplete((success,exception)->{
// 可以感知成功结果和异常,不能修改返回结果
System.out.println("异步任务完成,结果为"+success+",异常:"+exception);
}).exceptionally(throwable -> {
// 可以处理异常,提供默认值返回
return 10;
});
System.out.println("main**********end");
}
3 handler 方法执行后的处理,不管是堆结果还是对异常都可以修改返回结果
handler(BitFunction)
handlerAysnc(BitFunction)
handlerAysnc(BitFunction,Executor)
@Test
void test2() throws ExecutionException, InterruptedException {
System.out.println("main**********start");
Executor pool = getPool();
CompletableFuture<Serializable> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程**********" + Thread.currentThread().getId());
int num = 10 / 2;
System.out.println("结果**********" + num);
return num;
}, pool).handle((success, throwable) -> {
System.out.println(" handler 处理");
// 可以处理成功和异常
if (success != null) {
return success * 3;
}
if (throwable != null) {
return throwable;
}
return 10;
});
System.out.println("main**********end,返回结果"+ future.get());
}
4 线程串行化方法
thenApply(Function)
thenApplyAsync(Function)
thenApplyAsync(Function,Executor)
thenAccept(Consumer)
thenAcceptAsync(Consumer)
thenAcceptAsync(Consumer,Executor)
thenRun(Runnable)
thenRunAsync(Runnable)
thenRunAsync(Runnable,Executor)
thenApply 当一个线程依赖另一个线程时候获取上一个任务返回的结果,并返回当前任务的返回值。
thenAccept 消费处理结果,接收任务处理结果,并消费处理,无返回值
thenRun 只要上面方法执行完成,就开始执行thenRun,只处理任完任务后执行thenRun的后续操作。
带Async默认是异步执行,同之前
@Test
void test5() throws ExecutionException, InterruptedException {
System.out.println("main**********start");
Executor pool = getPool();
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程**********" + Thread.currentThread().getId());
int num = 10 / 2;
System.out.println("结果**********" + num);
return num;
}, pool).handle((success, throwable) -> {
System.out.println(" handler 处理");
// 可以处理成功和异常
if (success != null) {
return success * 3;
}
if (throwable != null) {
// 出现异常返回0
return 0;
}
return 0;
}).thenApplyAsync((res) -> {
System.out.println("任务二启动");
return "hello " + res;
}, pool);
System.out.println("main**********end,返回结果"+future.get());
}
5 两任务组合,都要完成
thenCombine(CompletionStage,BiFunction)
thenCombineAsync(CompletionStage,BiFunction)
thenCombineAsync(CompletionStage,BiFunction,Executor)
thenAcceptBoth(CompletionStage,BiConsumer)
thenAcceptBothAsync(CompletionStage,BiConsumer)
thenAcceptBothAsync(CompletionStage,Biconsumer,Executor)
runAfterBoth(CompletionStage,Runnable)
runAfterBothAsync(CompletionStage,Runnable)
runAfterBothAsync(CompletionStage,Runnable,Executor)
thenCombine 组合两个futrue,获取两个futrure返回结果,并返回当前任务的返回值。
thenAcceptBoth 组合两个future,获取两个future任务结果,然后处理任务,没有返回值
runAfterBoth 组合两个future,不需要获取future的结果,只需要两个future处理任务后,处理该任务
@Test
void test6() throws ExecutionException, InterruptedException {
System.out.println("main**********start");
Executor pool = getPool();
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程**********" + Thread.currentThread().getId());
int num = 10 / 2;
System.out.println("结果**********" + num);
return num;
}, pool).handle((success, throwable) -> {
System.out.println(" handler 处理");
// 可以处理成功和异常
if (success != null) {
return success * 3;
}
if (throwable != null) {
// 出现异常返回0
return 0;
}
return 0;
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
return "Hello";
}, pool);
CompletableFuture<String> combine = future1.thenCombine(future2, (num, str) -> {
return num + str;
});
System.out.println("main**********end,返回结果"+combine.get());
}
// 前两个任务都完成后开始执行任务三
future1.runAfterBothAsync(future2,()->{
System.out.println("任务三启动");
},pool);
6 两任务组合,一个完成
applyToEither(CompletionStage,Function)
applyToEitherAsync(CompletionStage,Function)
applyToEitherAsync(CompletionStage,Function,Executor)
acceptEither(CompletionStage,Consumer)
acceptEitherAsync(CompletionStage,Consumer)
acceptEitherAsync(CompletionStage,Consumer,Executor)
runAfterEither(CompletionStage,Runnable)
runAfterEitherAsync(CompletionStage,Runnable)
applyToEither 两个任务有一个执行成功,获取他的返回值,处理任务并且返回新的返回值
acceptEither 两个任务有一个执行成功,获取他的返回值,处理任务u,没有新的返回值
runAfterEither 两个任务有一个执行成功过,不需要获取future的结果,处理任务,也没有返回值。
@Test
void test7() throws ExecutionException, InterruptedException {
System.out.println("main**********start");
Executor pool = getPool();
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程**********" + Thread.currentThread().getId());
int num = 10 / 2;
System.out.println("结果**********" + num);
return num;
}, pool).handle((success, throwable) -> {
System.out.println(" handler 处理");
// 可以处理成功和异常
if (success != null) {
return success * 3;
}
if (throwable != null) {
// 出现异常返回0
return 0;
}
return 0;
});
CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
return "Hello";
}, pool);
CompletableFuture<String> toEither = future1.applyToEither(future2, str -> {
System.out.println("任务完成结果: "+str);
return str + "哈哈!";
});
System.out.println("两个任务执行完毕后返回最终任务结果 "+toEither.get());
}
7 多任务组合
static CompletableFuture allOf(CompletionStage…)
static CompletableFuture anyOf(CompletionStage…)
allOf 等待所有任务完成
anyOf 只要有一个任务完成
@Test
void test8() throws ExecutionException, InterruptedException {
System.out.println("main**********start");
Executor pool = getPool();
CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程**********" + Thread.currentThread().getId());
int num = 10 / 2;
System.out.println("结果**********" + num);
return num;
}, pool);
CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
return "Hello";
}, pool);
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
return "华为";
}, pool);
CompletableFuture<Void> future = CompletableFuture.allOf(future1, future2,future3);
future.get(); // 阻塞等待
Object o = future1.get();
Object o1 = future2.get();
String s = future3.get();
System.out.println("最终返回结果"+o+":"+o1+":"+s);
System.out.println("main**********end");
}