Java并行工作迭代器?

Java并行工作迭代器?,java,multithreading,concurrency,iterator,Java,Multithreading,Concurrency,Iterator,我正在寻找一个类,在这个类中,我可以重写一个方法来完成工作,并像迭代器一样返回结果。大概是这样的: ParallelWorkIterator<Result> itr = new ParallelWorkIterator<Result>(trials,threads) { public Result work() { //do work here for a single trial... return answer; } }; while (i

我正在寻找一个类,在这个类中,我可以重写一个方法来完成工作,并像迭代器一样返回结果。大概是这样的:

ParallelWorkIterator<Result> itr = new ParallelWorkIterator<Result>(trials,threads) {

  public Result work() {
    //do work here for a single trial...
    return answer;
  }

};
while (itr.hasNext()) {
  Result result = itr.next();
  //process result...
}
ParallelWorkIterator itr=新的ParallelWorkIterator(试用、线程){
公共成果工作(){
//在这里做一次试验。。。
返回答案;
}
};
while(itr.hasNext()){
结果=itr.next();
//过程结果。。。
}
这主要用于monte carlo模拟,但我不想每次都要设置线程池和管理返回的线程。我推出了我自己的类,希望能实现这一点,但我没有足够的信心,并认为我会检查,如果像这样的东西已经存在

编辑:为了清楚起见,我希望它在后台继续运行,并在每个工作方法返回后对结果进行排队,直到所有试验都完成。因此,下一个方法可能会等待返回,直到队列中出现结果。

请查看。它做你想做的一切

   void solve(Executor e, Collection<Callable<Result>> solvers)
     throws InterruptedException, ExecutionException {
       //This class will hold and execute your tasks
       CompletionService<Result> ecs
           = new ExecutorCompletionService<Result>(e);
       //Submit (start) all the tasks asynchronously
       for (Callable<Result> s : solvers)
           ecs.submit(s);
       //Retrieve completed task results and use them
       int n = solvers.size();
       for (int i = 0; i < n; ++i) {
           Result r = ecs.take().get();
           if (r != null)
               use(r);
       }
   }
void solve(执行器e、集合解算器)
抛出InterruptedException、ExecutionException{
//这个类将保存并执行您的任务
完成服务
=新的ExecutorCompletionService(e);
//异步提交(启动)所有任务
for(可调用的s:解算器)
ecs.提交;
//检索已完成的任务结果并使用它们
int n=solvers.size();
对于(int i=0;i

使用CompletionService的好处是它总是返回第一个完成的结果。这可以确保您不必等待任务完成,并且可以让未完成的任务在后台运行。

我建议您看看Java


您可以提交多个任务,并为每个任务返回一个对象。您的工作是在后台处理的,您可以在未来的对象中进行迭代(就像您在上面所做的那样)。每个Future都会在可用时返回一个结果(通过调用
get()
-这会一直阻止,直到结果在单独的线程中生成)

我能想到的最接近的方法是使用
CompletionService
在结果完成时累积结果

简单的例子:

ExecutorService executor = Executors.newSingleThreadExecutor(); // Create vanilla executor service.
CompletionService<Result> completionService = new ExecutorCompletionService<Result>(executor); // Completion service wraps executor and is notified of results as they complete.
Callable<Result> callable = new MyCallable();

executor.submit(callable); // Do not store handle to Future here but rather obtain from CompletionService when we *know* the result is complete.

Future<Result> fut = completionService.take(); // Will block until a completed result is available.
Result result = fut.get(); // Will not block as we know this future represents a completed result.

(名为
take()
的方法通常表示
java.util.concurrent
包中的阻塞调用)。

谢谢,看起来它完成了我想要的一切。我只是希望java不要那么冗长。冗长和明确是一把双刃剑的一半。我所做的就是编写一个包装类来扩展ExecutorCompletionService,并保留一个提交的未来列表。公共布尔值haslaining(){return0@Tim这是真的,我可以把它放在我已经写过的课堂上。
public interface BlockingResultSet {
  /**
   * Returns next result when it is ready, blocking is required.
   * Returns null if no more results are available.
   */  
  Result take() throws InterruptedException, ExecutionException;
}