Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. // From http://winterbe.com/posts/2015/05/22/java8-concurrency-tutorial-atomic-concurrent-map-examples/
  2. public class Test_AtomicInteger {
  3. public static void main(String[] args) {
  4. AtomicInteger atomicInt = new AtomicInteger(0);
  5.  
  6. ExecutorService executor = Executors.newFixedThreadPool(2);
  7.  
  8. IntStream.range(0, 1000)
  9. .forEach(i -> {
  10. Runnable task = () ->
  11. atomicInt.updateAndGet(n -> n + 2);
  12. executor.submit(task);
  13. });
  14.  
  15. executor.shutdownNow();
  16.  
  17. System.out.println(atomicInt.get()); // => 2000
  18. }
  19. }
Add Comment
Please, Sign In to add comment