複数の呼び出し可能オブジェクトを並列に実行したい。しかし、ExecutorServiceは、すべての呼び出し可能オブジェクトが終了するまで常に待機しているようです。
私は次のことを試しました:
final int nThreads = 10;
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
List<PrimeCallable> tasks = new ArrayList<PrimeCallable>();
for(int i = 0; i < nThreads; i++) {
tasks.add(new PrimeCallable(0, i * 100 + 100, "thread" + i));
}
try {
for(Future<List<Integer>> result : executorService.invokeAll(tasks)) {
List<Integer> integers = result.get();
for(Integer i : integers){
System.out.println(i);
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
これで、executorService内のすべての呼び出し可能オブジェクトが終了したときにforループが呼び出されます。私の知る限り、executorService.isParallelセッターはありません;-)。
呼び出し可能オブジェクトを並列実行させるための正しいアプローチは何でしょうか?
ヒントをありがとう!