[英]Java8 - CompletableFuture - Running methods in async call sequentially
[英]Java 8 - Call methods async in parallel and combine their results
我是新的Java 8并发功能,例如CompletableFuture
,我希望你可以帮助开始使用以下用例。
有一个名为TimeConsumingServices
的服务,它提供了耗时的操作,我想并行运行,因为它们都是独立的。
interface TimeConsumingService {
default String hello(String name) {
System.out.println(System.currentTimeMillis() + " > hello " + name);
return "Hello " + name;
}
default String planet(String name) {
System.out.println(System.currentTimeMillis() + " > planet " + name);
return "Planet: " + name;
}
default String echo(String name) {
System.out.println(System.currentTimeMillis() + " > echo " + name);
return name;
}
default byte[] convert(String hello, String planet, String echo) {
StringBuilder sb = new StringBuilder();
sb.append(hello);
sb.append(planet);
sb.append(echo);
return sb.toString().getBytes();
}
}
到目前为止,我实现了以下示例,并设法并行调用所有三种服务方法。
public class Runner implements TimeConsumingService {
public static void main(String[] args) {
new Runner().doStuffAsync();
}
public void doStuffAsync() {
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> this.hello("Friend"));
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> this.planet("Earth"));
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> this.echo("Where is my echo?"));
CompletableFuture.allOf(future1, future2, future3).join();
}
}
有没有办法收集每个服务调用的返回值并调用方法byte[]' convert(String, String, String)
?
要在返回结果后合并结果,您可以执行以下操作
CompletableFuture<byte[]> byteFuture = CompletableFuture.allOf(cf1, cf2, cf3)
.thenApplyAsync(aVoid -> convert(cf1.join(), cf2.join(), cf3.join()));
byte[] bytes = byteFuture.join();
这将运行你所有的期货,等待它们全部完成,然后一旦它们全部完成将调用你提到的convert
方法。
加入后,您可以简单地从future1
get()
值,如:
String s1 = future1.get()
等等
如果只有3个期货要完成,你可以使用thenCombine()
方法组合它们:
final CompletableFuture<byte[]> byteFuture = future1.thenCombine(future2, (t, u) -> {
StringBuilder sb = new StringBuilder();
sb.append(t);
sb.append(u);
return sb.toString();
}).thenCombine(future3, (t, u) -> {
StringBuilder sb = new StringBuilder();
sb.append(t);
sb.append(u);
return sb.toString();
}).thenApply(s -> s.getBytes());
try {
final byte[] get = byteFuture.get();
} catch (InterruptedException | ExecutionException ex) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.