问题描述
我有的列表CompletableFuture< MyService>
:
List<CompletableFuture<MyService>>
其中 MyService
是不可变的,如下所示:
where MyService
is immutable like the following:
public class MyService {
public boolean isAvailable() {
return true; // or false
}
}
我现在想要一个未来当其中一个期货完成时:
I now want a future that is completed when one of the futures:
- 完成; 和
- 对于未来提供的
MyService
实例:MyService。 isAvailable()
返回true
- is completed; and
- for the
MyService
instance as provided by that future:MyService.isAvailable()
returnstrue
进行处理时,我需要有可用的 MyService
实例。换句话说,我希望 CompletableFuture< MyService>
在满足这两个条件时完成。
When proceeding, I need to have the MyService
instance that is available. In other words, I want a CompletableFuture<MyService>
which completes when the two conditions are met.
怎么能我这样做?我知道我可以使用 CompletableFuture.anyOf(...)
在其中一个期货完成时继续进行,但我不确定如何整合第二个要求: MyService.isAvailable()
必须为true。
How can I do this? I know I can use CompletableFuture.anyOf(...)
to proceeed when one of the futures completes, but I am unsure how to integrate the second requirement: MyService.isAvailable()
must be true.
推荐答案
更新:我想我明白了现在的问题。它可以帮助您代理这样的期货吗?
Update: I think I understood your problem now. Would it help you to proxy your futures like this?
List<CompletableFuture<MyService>> givenFutures; // wherever they came from
CompletableFuture<MyService>[] myFutures = givenFutures.stream()
.map(future -> {
final CompletableFuture<MyService> futureWithCheck = new CompletableFuture<>();
future.thenAccept(myService -> {
if (myService.isAvailable()) {
futureWithCheck.complete(myService);
}
});
return futureWithCheck;})
.toArray(CompletableFuture[]::new);
// blocking
MyService availableService = (MyService) CompletableFuture.anyOf(myFutures).get();
// do what you want to do with the available service
更新2:我考虑过你关于 thenCompose
的问题,也许我的解决方案的中间部分可能表达如下:
Update 2: I thought about your question regarding thenCompose
and perhaps the middle part of my solution could be expressed like this:
CompletableFuture<MyService>[] myFutures = givenFutures.stream()
.map(future ->
future.thenCompose(myService ->
myService.isAvailable() ? CompletableFuture.completedFuture(myService) : new CompletableFuture<>()))
.toArray(CompletableFuture[]::new);
旧答案,完整性:
Old Answer, for completeness:
(会发表评论,但声誉太少)
(was gonna comment but I have too little reputation)
看起来你要么必须要投票 MyService.isAvailable()
重复并完成另一个 CompletableFuture
一旦返回true或 MyService
将(就像Didier L评论的那样)必须返回一个Future,它会在你提到的内部成员发生变化时跟踪并完成。因此,例如,所涉及的每个成员的设置者必须检查 isAvailable()
是否为真,如果是,则完成之前发出的所有期货。
Looks like you will either have to poll MyService.isAvailable()
repeatedly and complete another CompletableFuture
once this returns true or MyService
will (like Didier L commented) have to return a Future which it keeps track of and completes, once the internal members you mentioned are changed. So for example the setter for each member involved will have to check whether isAvailable()
is true and if so, complete all futures that it handed out before.
在这两种情况下,您都必须使用您拥有的附加 Future
。 Guavas 期货
有有用的方法,但可能 CompletableFuture.allOf(...)
将完成这项工作。
In both cases you have to chain the additional Future
with the ones you have. Guavas Futures
has useful methods, but probably CompletableFuture.allOf(…)
will do the job.
这篇关于如何在完成任何给定的CompletableFutures并且结果与某个谓词匹配时完成未来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!