java8 supplyasync_java-8 – CompletableFuture,supplyAsync()和thenApply()

这是不一样的事情.在第二个示例中,那么thenApply不被使用,确定对convertToB的调用在与方法doSomethingAndReturnA相同的线程中执行.

但是,在第一个例子中,当使用thenApply方法时,可能会发生其他事情.

首先,如果执行doSomethingAndReturnA的CompletableFuture已经完成,那么在调用者线程中会发生thenApply的调用.如果CompletableFutures尚未完成,则传递给thenApply的函数将在与doSomethingAndReturnA相同的线程中被调用.

混乱?哦this article might be helpful(感谢@SotiriosDelimanolis的链接).

我提供了一个简短的例子,说明如何应用.

public class CompletableTest {

public static void main(String... args) throws ExecutionException, InterruptedException {

final CompletableFuture future = CompletableFuture

.supplyAsync(() -> doSomethingAndReturnA())

.thenApply(a -> convertToB(a));

future.get();

}

private static int convertToB(final String a) {

System.out.println("convertToB: " + Thread.currentThread().getName());

return Integer.parseInt(a);

}

private static String doSomethingAndReturnA() {

System.out.println("doSomethingAndReturnA: " + Thread.currentThread().getName());

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

return "1";

}

}

输出的是:

doSomethingAndReturnA: ForkJoinPool.commonPool-worker-1

convertToB: ForkJoinPool.commonPool-worker-1

所以,当第一个操作很慢(即CompletableFuture尚未完成)时,两个调用发生在同一个线程中.但是如果我们从doSomethingAndReturnA中删除Thread.sleep调用,输出(可能)就是这样的:

doSomethingAndReturnA: ForkJoinPool.commonPool-worker-1

convertToB: main

请注意,convertToB调用在主线程中.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值