From ed51bc4cdaa9a6eff580e578aaeb0657303cff6d Mon Sep 17 00:00:00 2001 From: Anastasios Ioannidis Date: Fri, 20 Jan 2023 18:33:28 +0200 Subject: [PATCH] JAVA-17608 Restored AtomicInteger default increment method --- .../atomic/SafeCounterWithLock.java | 2 +- .../atomic/SafeCounterWithoutLock.java | 8 +----- .../ThreadSafeCounterIntegrationTest.java | 28 ++++++++++++++++--- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java index ef6b7ee8c8..09dfe575e6 100644 --- a/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java +++ b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithLock.java @@ -1,7 +1,7 @@ package com.baeldung.concurrent.atomic; public class SafeCounterWithLock { - private volatile int counter; + private int counter; int getValue() { return counter; diff --git a/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java index 8b2aebba7c..e1117dd842 100644 --- a/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java +++ b/core-java-modules/core-java-concurrency-advanced/src/main/java/com/baeldung/concurrent/atomic/SafeCounterWithoutLock.java @@ -10,12 +10,6 @@ public class SafeCounterWithoutLock { } void increment() { - while(true) { - int existingValue = getValue(); - int newValue = existingValue + 1; - if(counter.compareAndSet(existingValue, newValue)) { - return; - } - } + counter.incrementAndGet(); } } diff --git a/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterIntegrationTest.java b/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterIntegrationTest.java index c3c44b40cf..9b4b628d0f 100644 --- a/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterIntegrationTest.java +++ b/core-java-modules/core-java-concurrency-advanced/src/test/java/com/baeldung/concurrent/atomic/ThreadSafeCounterIntegrationTest.java @@ -17,8 +17,8 @@ public class ThreadSafeCounterIntegrationTest { SafeCounterWithLock safeCounter = new SafeCounterWithLock(); IntStream.range(0, 1000) - .forEach(count -> service.submit(safeCounter::increment)); - service.awaitTermination(100, TimeUnit.MILLISECONDS); + .forEach(count -> service.execute(safeCounter::increment)); + shutdownAndAwaitTermination(service); assertEquals(1000, safeCounter.getValue()); } @@ -29,10 +29,30 @@ public class ThreadSafeCounterIntegrationTest { SafeCounterWithoutLock safeCounter = new SafeCounterWithoutLock(); IntStream.range(0, 1000) - .forEach(count -> service.submit(safeCounter::increment)); - service.awaitTermination(100, TimeUnit.MILLISECONDS); + .forEach(count -> service.execute(safeCounter::increment)); + shutdownAndAwaitTermination(service); assertEquals(1000, safeCounter.getValue()); } + + private void shutdownAndAwaitTermination(ExecutorService pool) { + // Disable new tasks from being submitted + pool.shutdown(); + try { + // Wait a while for existing tasks to terminate + if (!pool.awaitTermination(100, TimeUnit.MILLISECONDS)) { + // Cancel currently executing tasks forcefully + pool.shutdownNow(); + // Wait a while for tasks to respond to being cancelled + if (!pool.awaitTermination(100, TimeUnit.MILLISECONDS)) + System.err.println("Pool did not terminate"); + } + } catch (InterruptedException ex) { + // (Re-)Cancel if current thread also interrupted + pool.shutdownNow(); + // Preserve interrupt status + Thread.currentThread().interrupt(); + } + } }