私はこの簡単なプログラムを使って、ThreadPoolとExecutorServiceを使って1から9までの数を数えます。 各スレッドは実行に1秒間待機しています。しかし、以下のプログラムでは、実行ごとにランダムな出力が得られます。 これを修正する方法がわからず、常に45を生成します。ExecutorServiceを使用した同時実行
助けてください!
public static void main(String[] args) throws InterruptedException {
AtomicLong count = new AtomicLong(0);
ExecutorService executor = Executors.newFixedThreadPool(10);
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
for(Integer i : list) {
executor.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count.set(count.get() + i);
}
});
}
System.out.println("Waiting...");
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
System.out.println("Total::"+count.get());
System.out.println("Done");
}
[競合条件とは?](https://stackoverflow.com/questions/34510/what-is-a-race-condition)の可能な複製 –