public class SynchroExample {
public final AtomicInteger integer = new AtomicInteger(0);
private ExecutorService service = Executors.newFixedThreadPool(100);
public void syn() {
for (int i = 0; i < 1000; i++) {
service.submit(() -> {
integer.incrementAndGet();
});
}
service.shutdown();
System.out.println(integer.get());
}
public static void main(String [] args) {
SynchroExample ex = new SynchroExample();
ex.syn();
}
}
Can someone please explain why this code doesn't work? I was under the impression that AtomicInteger is threadsafe. And this code is not returning 1000.