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"); + }); + } + } +}