2

我有一个存储在数组中的对象列表,每个对象都有一个简单的(非递归)任务来计算。

我想并行执行每个对象的这些方法,然后等待它们全部完成后再继续。

我可以知道下面的代码是否可以满足我的要求吗?有没有我忽略的问题?

谢谢。

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();

}
4

0 回答 0