CompletableFuture执行异步操作的几种方法


1 构造函数创建


最简单的方式就是通过构造函数创建一个CompletableFuture实例。如下代码所示。由于新创建的CompletableFuture还没有任何计算结果,这时调用join,当前线程会一直阻塞在这里。 

CompletableFuture<String> future = new CompletableFuture();
String result = future.join();
System.out.println(result);

此时,如果在另外一个线程中,主动设置该CompletableFuture的值,则上面线程中的结果就能返回。

future.complete("test");
这展示了CompletableFuture最简单的创建及使用方法。

2 supplyAsync创建


CompletableFuture.supplyAsync()也可以用来创建CompletableFuture实例。通过该函数创建的CompletableFuture实例会异步执行当前传入的计算任务。在调用端,则可以通过get或join获取最终计算结果。

supplyAsync有两种签名:

public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
 
 
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)

第一种只需传入一个Supplier实例(一般使用lamda表达式),此时框架会默认使用ForkJoin的线程池来执行被提交的任务。

第二种可以指定自定义的线程池,然后将任务提交给该线程池执行。

下面为使用supplyAsync创建CompletableFuture的示例:

CompletableFuture<String> future = CompletableFuture.supplyAsync(()->{
    System.out.println("compute test");
    return "test";
});
 
String result = future.join();
System.out.println("get result: " + result);


在示例中,异步任务中会打印出“compute test”,并返回"test"作为最终计算结果。所以,最终的打印信息为“get result: test”。

3 runAsync创建


CompletableFuture.runAsync()也可以用来创建CompletableFuture实例。与supplyAsync()不同的是,runAsync()传入的任务要求是Runnable类型的,所以没有返回值。因此,runAsync适合创建不需要返回值的计算任务。同supplyAsync()类似,runAsync()也有两种签名:

public static CompletableFuture<Void> runAsync(Runnable runnable)
 
 
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor)


 下面为使用runAsync()的例子:

CompletableFuture<Void> future = CompletableFuture.runAsync(()->{
    System.out.println("compute test");
});
System.out.println("get result: " + future.join());


在示例中,由于任务没有返回值, 所以最后的打印结果是"get result: null"。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`CompletableFuture` 提供了几种用于处理异步操作完成后的回调方法: ### 1. `thenAccept` 当异步操作完成后,此方法会被调用,它接收一个参数并执行相应操作。 ```java CompletableFuture.supplyAsync(() -> "Hello").thenAccept(result -> System.out.println(result)); ``` ### 2. `thenApply` 类似于 `thenAccept`,但它还接受一个转换函数作为参数,用于在回调时转换结果。 ```java CompletableFuture.supplyAsync(() -> "Hello").thenApply(str -> str.toUpperCase()).thenAccept(System.out::println); ``` ### 3. `thenCombine` 此方法允许您将两个 `CompletableFuture` 结果合并成一个新的 `CompletableFuture` 并执行回调。 ```java CompletableFuture<String> future1 = CompletableFuture.completedFuture("A"); CompletableFuture<String> future2 = CompletableFuture.completedFuture("B"); future1.thenCombine(future2, (a, b) -> a + b).thenAccept(System.out::println); // 输出 "AB" ``` ### 4. `thenRun` 与 `thenAccept` 类似,但不返回结果,主要用于执行某些操作。 ```java CompletableFuture.runAsync(() -> { System.out.println("Doing something..."); }); ``` ### 5. `whenComplete` 当异步操作完成时,此方法会被调用,并可以接收一个处理异常的方法以及另一个可选的操作方法。 ```java CompletableFuture.supplyAsync(() -> { throw new RuntimeException("Oops!"); }).whenComplete((result, ex) -> { if (ex != null) { System.err.println(ex.getMessage()); } }); ``` 这些回调方法可以帮助您更好地组织和管理异步操作的结果和错误处理流程。每个方法都有特定的应用场景,选择合适的回调可以根据您的具体需求优化代码结构和逻辑。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值