From 38f30e733fc9ecf70a79a90c65f2ed82947d05b4 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Thu, 2 Nov 2023 19:23:43 -0600 Subject: [PATCH] [BAEL-7074] Update tests --- .../atomicinteger/Employee.java | 1 - .../volatilekeyword/Employee.java | 29 ------------ .../SychronizeStaticDataUnitTest.java | 45 ++++++++----------- 3 files changed, 19 insertions(+), 56 deletions(-) delete mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/volatilekeyword/Employee.java diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/atomicinteger/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/atomicinteger/Employee.java index 845dd65ce6..9c3c9b987c 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/atomicinteger/Employee.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/atomicinteger/Employee.java @@ -23,7 +23,6 @@ public class Employee { private static void incrementCount() { count.incrementAndGet(); - System.out.println("Count = " + count.get()); } public static int getCount() { diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/volatilekeyword/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/volatilekeyword/Employee.java deleted file mode 100644 index f41b10e160..0000000000 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/volatilekeyword/Employee.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.concurrent.synchronizestatic.volatilekeyword; - -/** - * No synchronization. - */ -public class Employee -{ - - volatile static int count; - int id; - String name; - String title; - - public Employee(int id, String name, String title) - { - incrementCount(); - this.id = id; - this.name = name; - this.title = title; - } - - private static void incrementCount() { - System.out.println("Count = " + ++count); - } - - public static Integer getCount() { - return count; - } -} diff --git a/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/synchronizestatic/SychronizeStaticDataUnitTest.java b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/synchronizestatic/SychronizeStaticDataUnitTest.java index d8d9a344f1..7e5dca9f2b 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/synchronizestatic/SychronizeStaticDataUnitTest.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/synchronizestatic/SychronizeStaticDataUnitTest.java @@ -5,13 +5,22 @@ import org.junit.Test; import java.util.concurrent.Executor; import java.util.concurrent.Executors; +/** + * The tests in this class show the output of creating multiple + * types of Employee classes in the synchronizedstatic + * package. When not synchronized the out will not be sequential; + * when it is synchronized the output will be in sequential. + */ public class SychronizeStaticDataUnitTest { private final Executor pool = Executors.newFixedThreadPool(4); + private final int numberToTest = 100; + @Test public void whenNotSynchronized_thenDataOutOfOrder() { - int numberToTest = 100; + + System.out.println("No synchronization"); for(int i = 0; i < numberToTest; i++) { int finalI = i; @@ -22,22 +31,10 @@ public class SychronizeStaticDataUnitTest } } - @Test - public void whenVolatile_thenDataInOrder() { - int numberToTest = 100; - - for(int i = 0; i < numberToTest; i++) { - int finalI = i; - pool.execute(() -> - { - new com.baeldung.concurrent.synchronizestatic.volatilekeyword.Employee(finalI, "John", "Smith"); - }); - } - } - @Test public void whenSynchronizedMethod_thenDataInOrder() { - int numberToTest = 100; + + System.out.println("Synchronization with synchronized method"); for(int i = 0; i < numberToTest; i++) { int finalI = i; @@ -50,7 +47,8 @@ public class SychronizeStaticDataUnitTest @Test public void whenSynchronizedBlock_thenDataInOrder() { - int numberToTest = 100; + + System.out.println("Synchronization with synchronized block"); for(int i = 0; i < numberToTest; i++) { int finalI = i; @@ -63,20 +61,15 @@ public class SychronizeStaticDataUnitTest @Test public void whenAtomicInteger_thenDataInOrder() { - int numberToTest = 100; - - for(int i = 0; i < numberToTest; i++) { - int finalI = i; - pool.execute(() -> - { - new com.baeldung.concurrent.synchronizestatic.atomicinteger.Employee(finalI, "John", "Smith"); - }); - } + // Not straight forward to test this because we cannot log/print + // and increment values in a synchronized fashion like other + // tests } @Test public void whenReentrantLock_thenDataInOrder() { - int numberToTest = 100; + + System.out.println("Synchronization with ReentrantLock"); for(int i = 0; i < numberToTest; i++) { int finalI = i;