【发布时间】:2021-10-24 06:08:03
【问题描述】:
我对如何以非阻塞方式解决这种情况感到困惑。
考虑两个演员 Actor1 和 Actor2
Actor1内
Map<Int, Int> foo() {
List<String> finalList = foo_2();
Map<Int, Int> finalMap = // do stuff with finalList to get Map<Int, Int>;
return finalMap;
}
List<String> foo_2() {
CompletableFuture<List<String>> Querylist = ask(Actor2)
Querylist.get();
return QueryList;
}
当前在 foo_2 中,Querylist.get() 是一个阻塞调用。我想以某种非阻塞方式解决这个问题。我在Actor1 内部为Actor2 创建了一个消息适配器,因此Actor2 发送的任何消息都将由Actor1 处理。
我使用下面的方法来修改阻塞调用
Map<Int, Int> foo() {
CompletionStage<List<String>> finalList = foo_2();
finalList.whenComplete(
// what to do here?
)
// Map<Int, Int> finalMap = // do stuff with finalList to get Map<Int, Int>;
return finalMap;
}
CompletionStage<List<String>> foo_2() {
CompletionStage<List<String>> Querylist = ask(Actor2)
return QueryList;
}
我不确定如何正确使用 CompletionStage 构造来获得与阻塞 futures.get() 调用相同的结果。
【问题讨论】:
标签: java akka completable-future akka-cluster akka-typed