saw*_*303 10 java asynchronous java-8
我是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();
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我实现了以下示例,并设法并行调用所有三种服务方法.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法收集每个服务调用的返回值并调用该方法byte[]‘ convert(String, String, String)?
Ash*_*Ash 12
要在返回结果后合并结果,您可以执行以下操作
CompletableFuture<byte[]> byteFuture = CompletableFuture.allOf(cf1, cf2, cf3)
.thenApplyAsync(aVoid -> convert(cf1.join(), cf2.join(), cf3.join()));
byte[] bytes = byteFuture.join();
Run Code Online (Sandbox Code Playgroud)
这将运行你所有的期货,等待它们全部完成,然后一旦它们全部完成将调用convert你提到的方法.
加入后,您可以简单地get()从以下值中获取值future1:
String s1 = future1.get()
Run Code Online (Sandbox Code Playgroud)
等等
| 归档时间: |
|
| 查看次数: |
12310 次 |
| 最近记录: |