diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/atomicvsvolatile/SafeAtomicCounter.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/atomicvsvolatile/SafeAtomicCounter.java new file mode 100644 index 0000000000..196662e3ea --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/atomicvsvolatile/SafeAtomicCounter.java @@ -0,0 +1,16 @@ +package com.baeldung.atomicvsvolatile; + +import java.util.concurrent.atomic.AtomicInteger; + +public class SafeAtomicCounter { + + private final AtomicInteger counter = new AtomicInteger(0); + + public int getValue() { + return counter.get(); + } + + public void increment() { + counter.incrementAndGet(); + } +} diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/atomicvsvolatile/UnsafeCounter.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/atomicvsvolatile/UnsafeCounter.java new file mode 100644 index 0000000000..48094672e1 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/atomicvsvolatile/UnsafeCounter.java @@ -0,0 +1,14 @@ +package com.baeldung.atomicvsvolatile; + +public class UnsafeCounter { + + private int counter; + + public int getValue() { + return counter; + } + + public void increment() { + counter++; + } +} diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/atomicvsvolatile/UnsafeVolatileCounter.java b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/atomicvsvolatile/UnsafeVolatileCounter.java new file mode 100644 index 0000000000..6d06d5b12a --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-4/src/main/java/com/baeldung/atomicvsvolatile/UnsafeVolatileCounter.java @@ -0,0 +1,14 @@ +package com.baeldung.atomicvsvolatile; + +public class UnsafeVolatileCounter { + + private volatile int counter; + + public int getValue() { + return counter; + } + + public void increment() { + counter++; + } +} diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/test/java/com/baeldung/atomicvsvolatile/SafeAtomicCounterUnitTest.java b/core-java-modules/core-java-concurrency-advanced-4/src/test/java/com/baeldung/atomicvsvolatile/SafeAtomicCounterUnitTest.java new file mode 100644 index 0000000000..4eb65db716 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-4/src/test/java/com/baeldung/atomicvsvolatile/SafeAtomicCounterUnitTest.java @@ -0,0 +1,25 @@ +package com.baeldung.atomicvsvolatile; + +import static org.junit.Assert.assertEquals; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; +import org.junit.Test; + +public class SafeAtomicCounterUnitTest { + + private static final int INCREMENT_COUNTER = 1000; + private static final int TIMEOUT = 100; + private static final int POOL_SIZE = 3; + + @Test + public void givenMultiThread_whenSafeAtomicCounterIncrement() throws InterruptedException { + ExecutorService service = Executors.newFixedThreadPool(POOL_SIZE); + SafeAtomicCounter safeCounter = new SafeAtomicCounter(); + IntStream.range(0, INCREMENT_COUNTER).forEach(count -> service.submit(safeCounter::increment)); + service.awaitTermination(TIMEOUT, TimeUnit.MILLISECONDS); + assertEquals(INCREMENT_COUNTER, safeCounter.getValue()); + } +} \ No newline at end of file