我有一个存储在数组中的对象列表,每个对象都有一个简单的(非递归)任务来计算。
我想并行执行每个对象的那些方法,然后在继续之前等待所有这些方法完成。
我可以知道下面的代码是否符合我的要求?我有什么问题吗?
感谢。
public void calculateAllFitnessParallel()
{
ForkJoinPool pool = new ForkJoinPool();
for (int i = 0; i < currentPopulation; i++)
pool.execute(individuals[i]); // execute these in parallel
// close pool and wait
pool.shutdown();
for (int i = 0; i < currentPopulation; i++)
individuals[i].join();
// need all parallel tasks to finish before this
findFittest();
}