使用两个不同类型的Guava ListenableFutures的结果 [英] Use the results of two Guava ListenableFutures of different types
本文介绍了使用两个不同类型的Guava ListenableFutures的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个在其他线程上完成的ListenableFuture.每个未来都有不同的类型,我希望在它们都完成时使用它们的两个结果.
I have two ListenableFutures which are completed on other threads. Each future is of a different type, and I wish to use both of their results when they are both complete.
是否有一种优雅的方法可以使用番石榴来处理此问题?
Is there an elegant way to handle this using Guava?
推荐答案
Runnable listener = new Runnable() {
private boolean jobDone = false;
@Override
public synchronized void run() {
if (jobDone || !(future1.isDone() && future2.isDone())) {
return;
}
jobDone = true;
// TODO do your job
}
};
future1.addListener(listener);
future2.addListener(listener);
不太优雅,但是应该做.
Not really elegant, but should do the job.
或者,更优雅,但是您需要强制转换:
Or, more elegant, but you'll need casts:
ListenableFuture<List<Object>> composedFuture =
Futures.allAsList(future1, future2);
这篇关于使用两个不同类型的Guava ListenableFutures的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文