这是不一样的事情.在第二个示例中,那么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调用在主线程中.