- java多线程操作
- (使用)CompletableFuture
- 1、==runAsync和supplyAsync==
- 示例
- 2、在线程完成时的回调函数
- 示例
- 3、将线程串行化(thenApple)
- 示例
- 4、==thenCombine 合并任务==
- 示例
- 5、allOf 多种结合的调用
- 示例
public static CompletableFuturerunAsync(Runnable runnable) public static CompletableFuture runAsync(Runnable runnable, Executor executor) public static CompletableFuture supplyAsync(Supplier supplier) public static CompletableFuture supplyAsync(Supplier supplier, Executor executor)
-
runAsync没有返回值
-
supplyAsunc有返回值
-
Executor executor代表线程池,如果没有配置线程池将使用默认线程池(ForkJoinPool.commonPool())
CompletableFuture2、在线程完成时的回调函数>> f1 = CompletableFuture.supplyAsync(() -> this.mapper.accessNumByDay(Map), executor);
public CompletableFuturewhenComplete(BiConsumer super T,? super Throwable> action) public CompletableFuture whenCompleteAsync(BiConsumer super T,? super Throwable> action) public CompletableFuture whenCompleteAsync(BiConsumer super T,? super Throwable> action, Executor executor) public CompletableFuture exceptionally(Function fn)
- 可以看到Action的类型是BiConsumer super T,? super Throwable>它可以处理正常的计算结果,或者异常情况。
- super T为当前CompletableFuture的结果类型
- whenComplete 和 whenCompleteAsync 的区别:
- whenComplete:是执行当前任务的线程执行继续执行 whenComplete 的任务。
whenCompleteAsync:是执行把 whenCompleteAsync 这个任务继续提交给线程池来进行执行。
- whenComplete:是执行当前任务的线程执行继续执行 whenComplete 的任务。
f1.whenComplete((list, throwable) -> System.out.println("任务一:" + System.currentTimeMillis()));
3、将线程串行化(thenApple)
当一个线程的输入依赖另一个线程的输出时,将线程串行化
public CompletableFuture thenApply(Function super T,? extends U> fn) public CompletableFuture thenApplyAsync(Function super T,? extends U> fn) public CompletableFuture thenApplyAsync(Function super T,? extends U> fn, Executor executor)
- super T 上一个线程的返回值
- extends U 当先线程的返回值
private static void thenApply() throws Exception {
CompletableFuture future = CompletableFuture.supplyAsync(new Supplier() {
@Override
public Long get() {
long result = new Random().nextInt(100);
System.out.println("result1="+result);
return result;
}
}).thenApply(new Function() {
@Override
public Long apply(Long t) {
long result = t*5;
System.out.println("result2="+result);
return result;
}
});
long result = future.get();
System.out.println(result);
}
4、thenCombine 合并任务
将两个任务的结果合并
public CompletionStagethenCombine(CompletionStage extends U> other,BiFunction super T,? super U,? extends V> fn); public CompletionStage thenCombineAsync(CompletionStage extends U> other,BiFunction super T,? super U,? extends V> fn); public CompletionStage thenCombineAsync(CompletionStage extends U> other,BiFunction super T,? super U,? extends V> fn,Executor executor);
- 一个任务.thenCombine(另一个任务,(两个任务类型))
CompletableFuture5、allOf 多种结合的调用 示例>> f1 = CompletableFuture.supplyAsync(() -> this.mapper.accessNumByDay(Map), executor); CompletableFuture
>> f2 = CompletableFuture.supplyAsync(() -> this.mapper.accessGroup(Map), executor); CompletableFuture
@GetMapping("/getUserInfo5")
public String getUserInfo5() throws ExecutionException, InterruptedException {
CompletableFuture future1
= CompletableFuture.supplyAsync(() -> "biandan");
CompletableFuture future2
= CompletableFuture.supplyAsync(() -> "说");
CompletableFuture future3
= CompletableFuture.supplyAsync(() -> "让天下没有难写的代码");
//返回的是 void 类型
CompletableFuture combinedFuture_1
= CompletableFuture.allOf(future1, future2, future3);
//使用 join 方法手动获取结果
String combinedFuture_2 = Stream.of(future1, future2, future3)
.map(CompletableFuture::join)
.collect(Collectors.joining(" "));
return combinedFuture_2;
}
- 注意 CompletableFuture.allOf 的返回类型是CompletableFuture。这种方法的局限性在于它不能返回所有 Supplier 的组合结果。我们必须从未来手动获取结果。使用 CompletableFuture.join() 方法获取。



