From 02e02f928f4ff4e4bad974b1e3e30e17b2fbc8ed Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Thu, 2 Nov 2023 17:47:02 -0600 Subject: [PATCH 1/5] [BAEL-7074] Synchronization for static data examples --- .../retry/RetryCompletableFuture.java | 2 +- .../threadpool/CustomCompletableFuture.java | 8 +- .../atomicinteger/Employee.java | 32 +++++++ .../synchronizestatic/none/Employee.java | 28 ++++++ .../reentrantlock/Employee.java | 44 +++++++++ .../synchronizedblock/Employee.java | 34 +++++++ .../synchronizedmethod/Employee.java | 27 ++++++ .../volatilekeyword/Employee.java | 29 ++++++ .../SychronizeStaticDataUnitTest.java | 89 +++++++++++++++++++ 9 files changed, 288 insertions(+), 5 deletions(-) create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/atomicinteger/Employee.java create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/none/Employee.java create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/reentrantlock/Employee.java create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedblock/Employee.java create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedmethod/Employee.java create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/volatilekeyword/Employee.java create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/synchronizestatic/SychronizeStaticDataUnitTest.java diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java index a3df6b3624..a46665559f 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java @@ -48,7 +48,7 @@ public class RetryCompletableFuture { CompletableFuture cf = CompletableFuture.supplyAsync(supplier); sleep(100); for (int i = 0; i < maxRetries; i++) { - cf = cf.exceptionallyAsync(__ -> supplier.get()); + //cf = cf.exceptionallyAsync(__ -> supplier.get()); } return cf; } diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/threadpool/CustomCompletableFuture.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/threadpool/CustomCompletableFuture.java index 1f3997768e..473cc562ca 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/threadpool/CustomCompletableFuture.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/threadpool/CustomCompletableFuture.java @@ -20,9 +20,9 @@ public class CustomCompletableFuture extends CompletableFuture { return future; } - @Override - public Executor defaultExecutor() { - return executor; - } +// @Override +// public Executor defaultExecutor() { +// return executor; +// } } \ No newline at end of file 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 new file mode 100644 index 0000000000..845dd65ce6 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/atomicinteger/Employee.java @@ -0,0 +1,32 @@ +package com.baeldung.concurrent.synchronizestatic.atomicinteger; + +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Synchronizing static variable with AtomicInteger. + */ +public class Employee { + + private static final AtomicInteger count = new AtomicInteger(0); + + 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() { + count.incrementAndGet(); + System.out.println("Count = " + count.get()); + } + + public static int getCount() { + return count.get(); + } +} diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/none/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/none/Employee.java new file mode 100644 index 0000000000..f050a68be4 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/none/Employee.java @@ -0,0 +1,28 @@ +package com.baeldung.concurrent.synchronizestatic.none; + +/** + * No synchronization. + */ +public class Employee { + + 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/main/java/com/baeldung/concurrent/synchronizestatic/reentrantlock/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/reentrantlock/Employee.java new file mode 100644 index 0000000000..2ac0b0d3f2 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/reentrantlock/Employee.java @@ -0,0 +1,44 @@ +package com.baeldung.concurrent.synchronizestatic.reentrantlock; + +import java.util.concurrent.locks.ReentrantLock; + +/** + * Synchronizing static variable with a Reenatrant Lock. + */ +public class Employee { + + private static final ReentrantLock lock = new ReentrantLock(); + + 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() { + lock.lock(); + try { + System.out.println("Count = " + ++count); + } + finally { + lock.unlock(); + } + } + + public static int getCount() { + lock.lock(); + try { + return count; + } + finally { + lock.unlock(); + } + } +} diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedblock/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedblock/Employee.java new file mode 100644 index 0000000000..9c078e7abe --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedblock/Employee.java @@ -0,0 +1,34 @@ +package com.baeldung.concurrent.synchronizestatic.synchronizedblock; + +/** + * Synchronizing static variable with a synchronized block. + */ +public class Employee { + + private static final Object lock = new Object(); + + 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() { + synchronized(lock) { + System.out.println("Count = " + ++count); + } + } + + public static int getCount() { + synchronized(lock) { + return count; + } + } +} diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedmethod/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedmethod/Employee.java new file mode 100644 index 0000000000..f0a785c427 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedmethod/Employee.java @@ -0,0 +1,27 @@ +package com.baeldung.concurrent.synchronizestatic.synchronizedmethod; + +/** + * Synchronizing static variable with a synchronized method. + */ +public class Employee { + 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 synchronized void incrementCount() { + System.out.println("Count = " + ++count); + } + + public static synchronized int getCount() { + return count; + } +} 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 new file mode 100644 index 0000000000..f41b10e160 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/volatilekeyword/Employee.java @@ -0,0 +1,29 @@ +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 new file mode 100644 index 0000000000..d8d9a344f1 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/synchronizestatic/SychronizeStaticDataUnitTest.java @@ -0,0 +1,89 @@ +package com.baeldung.concurrent.synchronizestatic; + +import org.junit.Test; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +public class SychronizeStaticDataUnitTest +{ + private final Executor pool = Executors.newFixedThreadPool(4); + + @Test + public void whenNotSynchronized_thenDataOutOfOrder() { + int numberToTest = 100; + + for(int i = 0; i < numberToTest; i++) { + int finalI = i; + pool.execute(() -> + { + new com.baeldung.concurrent.synchronizestatic.none.Employee(finalI, "John", "Smith"); + }); + } + } + + @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; + + for(int i = 0; i < numberToTest; i++) { + int finalI = i; + pool.execute(() -> + { + new com.baeldung.concurrent.synchronizestatic.synchronizedmethod.Employee(finalI, "John", "Smith"); + }); + } + } + + @Test + public void whenSynchronizedBlock_thenDataInOrder() { + int numberToTest = 100; + + for(int i = 0; i < numberToTest; i++) { + int finalI = i; + pool.execute(() -> + { + new com.baeldung.concurrent.synchronizestatic.synchronizedblock.Employee(finalI, "John", "Smith"); + }); + } + } + + @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"); + }); + } + } + + @Test + public void whenReentrantLock_thenDataInOrder() { + int numberToTest = 100; + + for(int i = 0; i < numberToTest; i++) { + int finalI = i; + pool.execute(() -> + { + new com.baeldung.concurrent.synchronizestatic.reentrantlock.Employee(finalI, "John", "Smith"); + }); + } + } +} From 38f30e733fc9ecf70a79a90c65f2ed82947d05b4 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Thu, 2 Nov 2023 19:23:43 -0600 Subject: [PATCH 2/5] [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; From 1aa2f849f49c5f14c25e07966e199593ca449e80 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Fri, 3 Nov 2023 13:10:24 -0600 Subject: [PATCH 3/5] Revert 2 changes unrelated to BAEL-7074 --- .../completablefuture/retry/RetryCompletableFuture.java | 2 +- .../threadpool/CustomCompletableFuture.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java index a46665559f..a3df6b3624 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java @@ -48,7 +48,7 @@ public class RetryCompletableFuture { CompletableFuture cf = CompletableFuture.supplyAsync(supplier); sleep(100); for (int i = 0; i < maxRetries; i++) { - //cf = cf.exceptionallyAsync(__ -> supplier.get()); + cf = cf.exceptionallyAsync(__ -> supplier.get()); } return cf; } diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/threadpool/CustomCompletableFuture.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/threadpool/CustomCompletableFuture.java index 473cc562ca..1f3997768e 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/threadpool/CustomCompletableFuture.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/threadpool/CustomCompletableFuture.java @@ -20,9 +20,9 @@ public class CustomCompletableFuture extends CompletableFuture { return future; } -// @Override -// public Executor defaultExecutor() { -// return executor; -// } + @Override + public Executor defaultExecutor() { + return executor; + } } \ No newline at end of file From 159461815d99562f7d8f1babe787c7f784145a92 Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Fri, 3 Nov 2023 15:31:25 -0600 Subject: [PATCH 4/5] New example and unit test --- .../synchronizedclass/Employee.java | 32 +++++++++++++++++++ .../SychronizeStaticDataUnitTest.java | 16 +++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedclass/Employee.java diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedclass/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedclass/Employee.java new file mode 100644 index 0000000000..a69b633f3b --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedclass/Employee.java @@ -0,0 +1,32 @@ +package com.baeldung.concurrent.synchronizestatic.synchronizedclass; + +/** + * Synchronizing static variable with a synchronized block. + */ +public class Employee +{ + 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() { + synchronized(Employee.class) { + System.out.println("Count = " + ++count); + } + } + + public static int getCount() { + synchronized(Employee.class) { + 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 7e5dca9f2b..e633005c6f 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 @@ -45,10 +45,24 @@ public class SychronizeStaticDataUnitTest } } + @Test + public void whenSynchronizedClass_thenDataInOrder() { + + System.out.println("Synchronization with synchronized block on class"); + + for(int i = 0; i < numberToTest; i++) { + int finalI = i; + pool.execute(() -> + { + new com.baeldung.concurrent.synchronizestatic.synchronizedclass.Employee(finalI, "John", "Smith"); + }); + } + } + @Test public void whenSynchronizedBlock_thenDataInOrder() { - System.out.println("Synchronization with synchronized block"); + System.out.println("Synchronization with synchronized block on a private object"); for(int i = 0; i < numberToTest; i++) { int finalI = i; From 573df300b06ea130e8d4762c71f4072ab0cfd81c Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Sun, 12 Nov 2023 09:00:56 -0700 Subject: [PATCH 5/5] Fix indentation issues --- .../atomicinteger/Employee.java | 3 +- .../synchronizestatic/none/Employee.java | 3 +- .../reentrantlock/Employee.java | 3 +- .../synchronizedblock/Employee.java | 3 +- .../synchronizedclass/Employee.java | 3 +- .../SychronizeStaticDataUnitTest.java | 38 ++++++++----------- 6 files changed, 21 insertions(+), 32 deletions(-) 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 9c3c9b987c..bb7c1c6b4c 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 @@ -13,8 +13,7 @@ public class Employee { String name; String title; - public Employee(int id, String name, String title) - { + public Employee(int id, String name, String title) { incrementCount(); this.id = id; this.name = name; diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/none/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/none/Employee.java index f050a68be4..ba722e15e9 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/none/Employee.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/none/Employee.java @@ -10,8 +10,7 @@ public class Employee { String name; String title; - public Employee(int id, String name, String title) - { + public Employee(int id, String name, String title) { incrementCount(); this.id = id; this.name = name; diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/reentrantlock/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/reentrantlock/Employee.java index 2ac0b0d3f2..e74013ad0e 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/reentrantlock/Employee.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/reentrantlock/Employee.java @@ -14,8 +14,7 @@ public class Employee { String name; String title; - public Employee(int id, String name, String title) - { + public Employee(int id, String name, String title) { incrementCount(); this.id = id; this.name = name; diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedblock/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedblock/Employee.java index 9c078e7abe..4cf04e5b06 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedblock/Employee.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedblock/Employee.java @@ -12,8 +12,7 @@ public class Employee { String name; String title; - public Employee(int id, String name, String title) - { + public Employee(int id, String name, String title) { incrementCount(); this.id = id; this.name = name; diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedclass/Employee.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedclass/Employee.java index a69b633f3b..627040e88c 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedclass/Employee.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/synchronizestatic/synchronizedclass/Employee.java @@ -10,8 +10,7 @@ public class Employee String name; String title; - public Employee(int id, String name, String title) - { + public Employee(int id, String name, String title) { incrementCount(); this.id = id; this.name = name; 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 e633005c6f..b4e03673a1 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 @@ -11,8 +11,7 @@ import java.util.concurrent.Executors; * package. When not synchronized the out will not be sequential; * when it is synchronized the output will be in sequential. */ -public class SychronizeStaticDataUnitTest -{ +public class SychronizeStaticDataUnitTest { private final Executor pool = Executors.newFixedThreadPool(4); private final int numberToTest = 100; @@ -24,10 +23,9 @@ public class SychronizeStaticDataUnitTest for(int i = 0; i < numberToTest; i++) { int finalI = i; - pool.execute(() -> - { - new com.baeldung.concurrent.synchronizestatic.none.Employee(finalI, "John", "Smith"); - }); + pool.execute(() -> { + new com.baeldung.concurrent.synchronizestatic.none.Employee(finalI, "John", "Smith"); + }); } } @@ -38,10 +36,9 @@ public class SychronizeStaticDataUnitTest for(int i = 0; i < numberToTest; i++) { int finalI = i; - pool.execute(() -> - { - new com.baeldung.concurrent.synchronizestatic.synchronizedmethod.Employee(finalI, "John", "Smith"); - }); + pool.execute(() -> { + new com.baeldung.concurrent.synchronizestatic.synchronizedmethod.Employee(finalI, "John", "Smith"); + }); } } @@ -52,10 +49,9 @@ public class SychronizeStaticDataUnitTest for(int i = 0; i < numberToTest; i++) { int finalI = i; - pool.execute(() -> - { - new com.baeldung.concurrent.synchronizestatic.synchronizedclass.Employee(finalI, "John", "Smith"); - }); + pool.execute(() -> { + new com.baeldung.concurrent.synchronizestatic.synchronizedclass.Employee(finalI, "John", "Smith"); + }); } } @@ -66,10 +62,9 @@ public class SychronizeStaticDataUnitTest for(int i = 0; i < numberToTest; i++) { int finalI = i; - pool.execute(() -> - { - new com.baeldung.concurrent.synchronizestatic.synchronizedblock.Employee(finalI, "John", "Smith"); - }); + pool.execute(() -> { + new com.baeldung.concurrent.synchronizestatic.synchronizedblock.Employee(finalI, "John", "Smith"); + }); } } @@ -87,10 +82,9 @@ public class SychronizeStaticDataUnitTest for(int i = 0; i < numberToTest; i++) { int finalI = i; - pool.execute(() -> - { - new com.baeldung.concurrent.synchronizestatic.reentrantlock.Employee(finalI, "John", "Smith"); - }); + pool.execute(() -> { + new com.baeldung.concurrent.synchronizestatic.reentrantlock.Employee(finalI, "John", "Smith"); + }); } } }