Нужно распараллелить цикл. На каждой терации цикла выполняется некоторый метод, который сам по себе занимает около 2-х секунд времени на моей машине. Решил распараллелить.
Почему-то получилось так, что "параллельное" время соизмеримо с последовательным временем. Появляется мысль, что цикл не распаралеллен. В чем проблема?
int n = 10;
int totalStepsNumber = n*n*n;
long start = System.currentTimeMillis();
try{
PrintWriter output = new PrintWriter(new FileOutputStream("result.txt"));
ExecutorService executer = Executors.newFixedThreadPool(2);
List<Maker> makerList = new ArrayList<Maker>();
for ( int i = 0; i < n; i++ ){
for ( int j = 0; j < n; j++ ){
for ( int k = 0; k < n; k++ ){
makerList.add(new Maker(n, i, j, k));
int currentStep = i*n*n + j*n + k + 1;
System.out.println(currentStep + "/" + totalStepsNumber);
}
}
}
List<Future<Double>> res = executer.invokeAll(makerList);
for ( int i = 0; i < res.size(); i++ ){
output.println(res.get(i).get());
}
executer.shutdown();
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println(end-start);