このためにどこかに定義された共通のFuture拡張機能はありますか?
からの増分結果について話していると思いますExecutorService
。オブジェクトExecutorCompletionService
の1つが取得可能になるとすぐに通知されるようにするanの使用を検討する必要があります。Future
javadocsから引用するには:
CompletionService<Result> ecs = new ExecutorCompletionService<Result>(e);
for (Callable<Result> s : solvers) {
ecs.submit(s);
}
int n = solvers.size();
for (int i = 0; i < n; ++i) {
// this waits for one of the futures to finish and provide a result
Future<Result> future = ecs.take();
Result result = future.get();
if (result != null) {
// do something with the result
}
}
ごめん。私は最初に質問を読み間違え、あなたがリスト<未来<?>>について質問していると思いました。コードをリファクタリングして実際に多くの先物を返すことができるかもしれないので、これは後世のために残しておきます。
この場合、リストを返しませんFuture
。仕事が終わるまで、あなたはリターンを得ることができないでしょう。
可能であれば、呼び出し元とスレッドの両方がアクセスできるように、ある種のパスを渡します。BlockingQueue
final BlockingQueue<T> queue = new LinkedBlockingQueue<T>();
// build out job with the queue
threadPool.submit(new SomeJob(queue));
threadPool.shutdown();
// now we can consume from the queue as it is built:
while (true) {
T result = queue.take();
// you could some constant result object to mean that the job finished
if (result == SOME_END_OBJECT) {
break;
}
// provide intermediate results
}
また、ジョブクラス内で定義されSomeJob.take()
た内部を呼び出すある種のメソッドを持つこともできます。BlockingQueue
// the blocking queue in this case is hidden inside your job object
T result = someJob.take();
...