面试碰到一个问题,搞不定
CompletableFuture<String> futureC = CompletableFuture.supplyAsync(() -> “c”);
CompletableFuture<String> futureD = CompletableFuture.supplyAsync(() -> “d”);
CompletableFuture<String> futureE = CompletableFuture.supplyAsync(() -> “e”);
String collect = Stream.of(futureAb, futureC, futureD, futureE)
.map(CompletableFuture::join)
.collect(Collectors.joining(” “));
//output: ab c d e
System.out.println(collect);
“`
因为整合数据时,需要保证 bcde 请求调用完成,所以就用一下 CountDownLatch 来实现这个等待机制。
其他细节上的问题,上的老哥们已经说了。
如果 a processing time 短于 b 还可以。
x->b and x->a precall
b processing and then retrieve ->a response
response x
这样的时间 和 x->a round trip + a prossing time 是一样的