BAEL-7145

Bill Pugh Singleton implementation
This commit is contained in:
parthiv39731 2023-10-28 21:11:08 +05:30
parent 29d0e4f04c
commit 462ddc7f02
8 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package com.baledung.billpugh;
public class BillPughSingleton {
private BillPughSingleton() {
}
private static class SingletonHelper {
private static final BillPughSingleton BILL_PUGH_SINGLETON_INSTANCE = new BillPughSingleton();
}
public static BillPughSingleton getInstance() {
return SingletonHelper.BILL_PUGH_SINGLETON_INSTANCE;
}
}

View File

@ -0,0 +1,11 @@
package com.baledung.billpugh;
public class EagerLoadedSingleton {
private static final EagerLoadedSingleton EAGER_LOADED_SINGLETON = new EagerLoadedSingleton();
private EagerLoadedSingleton() {
}
public static EagerLoadedSingleton getInstance() {
return EAGER_LOADED_SINGLETON;
}
}

View File

@ -0,0 +1,15 @@
package com.baledung.billpugh;
public class LazyLoadedSingleton {
private static LazyLoadedSingleton lazyLoadedSingletonObj;
private LazyLoadedSingleton() {
}
public static LazyLoadedSingleton getInstance() {
if (null == lazyLoadedSingletonObj) {
lazyLoadedSingletonObj = new LazyLoadedSingleton();
}
return lazyLoadedSingletonObj;
}
}

View File

@ -0,0 +1,15 @@
package com.baledung.billpugh;
public class SynchronizedLazyLoadedSingleton {
private static SynchronizedLazyLoadedSingleton synchronizedLazyLoadedSingleton;
private SynchronizedLazyLoadedSingleton() {
}
public static synchronized SynchronizedLazyLoadedSingleton getInstance() {
if (null == synchronizedLazyLoadedSingleton) {
synchronizedLazyLoadedSingleton = new SynchronizedLazyLoadedSingleton();
}
return synchronizedLazyLoadedSingleton;
}
}

View File

@ -0,0 +1,43 @@
package com.baledung.billpugh;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class BillPughSingletonUnitTest {
Logger logger = LoggerFactory.getLogger(BillPughSingletonUnitTest.class);
@Test
void giveSynchronizedLazyLoadedImpl_whenCallgetInstance_thenReturnSingleton() {
Set<BillPughSingleton> setHoldingSingletonObj = new HashSet<>();
List<Future<BillPughSingleton>> futures = new ArrayList<>();
ExecutorService executorService = Executors.newFixedThreadPool(10);
Callable<BillPughSingleton> runnableTask = () -> {
logger.info("run called for:" + Thread.currentThread().getName());
return BillPughSingleton.getInstance();
};
int count = 0;
while(count < 10) {
futures.add(executorService.submit(runnableTask));
count++;
}
futures.forEach(e -> {
try {
setHoldingSingletonObj.add(e.get());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
});
executorService.shutdown();
assertEquals(1, setHoldingSingletonObj.size());
}
}

View File

@ -0,0 +1,46 @@
package com.baledung.billpugh;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EagerLoadedSingletonUnitTest {
Logger logger = LoggerFactory.getLogger(EagerLoadedSingletonUnitTest.class);
@Test
void giveEagerLoadedImpl_whenCallgetInstance_thenReturnSingleton() {
Set<EagerLoadedSingleton> set = new HashSet<>();
List<Future<EagerLoadedSingleton>> futures = new ArrayList<>();
ExecutorService executorService = Executors.newFixedThreadPool(10);
Callable<EagerLoadedSingleton> runnableTask = () -> {
logger.info("run called for:" + Thread.currentThread().getName());
return EagerLoadedSingleton.getInstance();
};
int count = 0;
while(count < 10) {
futures.add(executorService.submit(runnableTask));
count++;
}
futures.forEach(e -> {
try {
set.add(e.get());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
});
executorService.shutdown();
assertEquals(1, set.size());
}
}

View File

@ -0,0 +1,17 @@
package com.baledung.billpugh;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class LazyLoadedSingletonUnitTest {
@Test
void givenLazyLoadedImpl_whenCallGetInstance_thenReturnSingleInstance() throws ClassNotFoundException {
Class bs = Class.forName("com.baledung.billpugh.LazyLoadedSingleton");
assertThrows(IllegalAccessException.class, () -> bs.getDeclaredConstructor().newInstance());
LazyLoadedSingleton lazyLoadedSingletonObj1 = LazyLoadedSingleton.getInstance();
LazyLoadedSingleton lazyLoadedSingletonObj2 = LazyLoadedSingleton.getInstance();
assertEquals(lazyLoadedSingletonObj1.hashCode(), lazyLoadedSingletonObj2.hashCode());
}
}

View File

@ -0,0 +1,44 @@
package com.baledung.billpugh;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SynchronizedLazyLoadedSingletonUnitTest {
Logger logger = LoggerFactory.getLogger(SynchronizedLazyLoadedSingletonUnitTest.class);
@Test
void giveSynchronizedLazyLoadedImpl_whenCallgetInstance_thenReturnSingleton() {
Set<SynchronizedLazyLoadedSingleton> setHoldingSingletonObj = new HashSet<>();
List<Future<SynchronizedLazyLoadedSingleton>> futures = new ArrayList<>();
ExecutorService executorService = Executors.newFixedThreadPool(10);
Callable<SynchronizedLazyLoadedSingleton> runnableTask = () -> {
logger.info("run called for:" + Thread.currentThread().getName());
return SynchronizedLazyLoadedSingleton.getInstance();
};
int count = 0;
while(count < 10) {
futures.add(executorService.submit(runnableTask));
count++;
}
futures.forEach(e -> {
try {
setHoldingSingletonObj.add(e.get());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
});
executorService.shutdown();
assertEquals(1, setHoldingSingletonObj.size());
}
}