Quantcast
Channel: Multithreading with AtomicInteger and ExecutorService not working - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Multithreading with AtomicInteger and ExecutorService not working

$
0
0
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.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images