我想执行多个可并行调用对象。但似乎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的可调用的finnished for循环被调用。据我所知,没有executorService.isParallel setter ;-)。
什么是正确的方法让可调参数平行运行?
感谢您的提示!