commit
2309c3e0cb
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 givenSynchronizedLazyLoadedImpl_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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue