[BAEL-7074] Synchronization for static data examples

This commit is contained in:
Michael Pratt 2023-11-02 17:47:02 -06:00
parent 57840bb083
commit 02e02f928f
9 changed files with 288 additions and 5 deletions

View File

@ -48,7 +48,7 @@ public class RetryCompletableFuture {
CompletableFuture<T> cf = CompletableFuture.supplyAsync(supplier); CompletableFuture<T> cf = CompletableFuture.supplyAsync(supplier);
sleep(100); sleep(100);
for (int i = 0; i < maxRetries; i++) { for (int i = 0; i < maxRetries; i++) {
cf = cf.exceptionallyAsync(__ -> supplier.get()); //cf = cf.exceptionallyAsync(__ -> supplier.get());
} }
return cf; return cf;
} }

View File

@ -20,9 +20,9 @@ public class CustomCompletableFuture<T> extends CompletableFuture<T> {
return future; return future;
} }
@Override // @Override
public Executor defaultExecutor() { // public Executor defaultExecutor() {
return executor; // return executor;
} // }
} }

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

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