Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // From http://winterbe.com/posts/2015/05/22/java8-concurrency-tutorial-atomic-concurrent-map-examples/
- public class Test_AtomicInteger {
- public static void main(String[] args) {
- AtomicInteger atomicInt = new AtomicInteger(0);
- ExecutorService executor = Executors.newFixedThreadPool(2);
- IntStream.range(0, 1000)
- .forEach(i -> {
- Runnable task = () ->
- atomicInt.updateAndGet(n -> n + 2);
- executor.submit(task);
- });
- executor.shutdownNow();
- System.out.println(atomicInt.get()); // => 2000
- }
- }
Add Comment
Please, Sign In to add comment