次のサンプルコードがあります。
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.concurrent.*;
public class CalculationThread implements Callable<Long> {
public Long call() throws Exception {
long k=0L;
for(int i=0;i<100000;i++){
for(int j=0;j<50;j++){
k=i+j;
}
}
return k;
}
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(4);
long startTime = System.nanoTime();
for(int lo=0;lo<5000;lo++){
Future<Long> result = executorService.submit(new CalculationThread());
try {
Long l = result.get();
} catch (Exception e) {
result.cancel(true);
}
}
long endTime = System.nanoTime();
System.out.println("Using threads took "+(endTime - startTime) + " ns");
executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.SECONDS);
long k=0L;
startTime = System.nanoTime();
for(int lo=0;lo<5000;lo++){
for(int i=0;i<100000;i++){
for(int j=0;j<50;j++){
k=i+j;
}
}
}
endTime = System.nanoTime();
System.out.println("Generally it takes "+(endTime - startTime) + " ns");
}
}
出力は次のように広がります
Using threads took 101960490 ns
Generally it takes 143107865 ns
に
Using threads took 245339720 ns
Generally it takes 149699885 ns
お気づきのように、2 番目の行はほぼ一定ですが、スレッド化されたバージョンは大きく異なります。なぜそのような場合があるのですか?変動性を減らすために何ができるでしょうか?私はJavaマルチスレッドに慣れていないので、何か愚かなことをしている場合はお知らせください。