Java 11 migrate all remaining s (#1120)
* Moves saga to Java 11 * Moves semaphore to Java 11 * Moves servant to Java 11 * Moves serverless to Java 11 * Moves service-layer to Java 11 * Moves service-locator to Java 11 * Moves sharding to Java 11 * Moves singleton to Java 11 * Moves spatial-partition to Java 11 * Moves specification to Java 11 * Moves state to Java 11 * Moves step-builder to Java 11 * Moves strategy to Java 11 * Moves subclass-sandbox to Java 11 * Fixes checkstyle issues
This commit is contained in:
committed by
Ilkka Seppälä
parent
310ae50248
commit
cd2a2e7711
@ -45,8 +45,7 @@ import org.slf4j.LoggerFactory;
|
||||
* example is found in {@link EnumIvoryTower}. At first glance the code looks short and simple.
|
||||
* However, you should be aware of the downsides including committing to implementation strategy,
|
||||
* extending the enum class, serializability and restrictions to coding. These are extensively
|
||||
* discussed in Stack Overflow:
|
||||
* http://programmers.stackexchange.com/questions/179386/what-are-the-downsides-of-implementing
|
||||
* discussed in Stack Overflow: http://programmers.stackexchange.com/questions/179386/what-are-the-downsides-of-implementing
|
||||
* -a-singleton-with-javas-enum</p>
|
||||
*
|
||||
* <p>{@link ThreadSafeLazyLoadedIvoryTower} is a Singleton implementation that is initialized on
|
||||
@ -54,9 +53,9 @@ import org.slf4j.LoggerFactory;
|
||||
* synchronized.</p>
|
||||
*
|
||||
* <p>Another Singleton implementation that is initialized on demand is found in
|
||||
* {@link ThreadSafeDoubleCheckLocking}. It is somewhat faster than
|
||||
* {@link ThreadSafeLazyLoadedIvoryTower} since it doesn't synchronize the whole access method but
|
||||
* only the method internals on specific conditions.</p>
|
||||
* {@link ThreadSafeDoubleCheckLocking}. It is somewhat faster than {@link
|
||||
* ThreadSafeLazyLoadedIvoryTower} since it doesn't synchronize the whole access method but only the
|
||||
* method internals on specific conditions.</p>
|
||||
*
|
||||
* <p>Yet another way to implement thread safe lazily initialized Singleton can be found in
|
||||
* {@link InitializingOnDemandHolderIdiom}. However, this implementation requires at least Java 8
|
||||
@ -80,10 +79,8 @@ public class App {
|
||||
LOGGER.info("ivoryTower2={}", ivoryTower2);
|
||||
|
||||
// lazily initialized singleton
|
||||
var threadSafeIvoryTower1 =
|
||||
ThreadSafeLazyLoadedIvoryTower.getInstance();
|
||||
var threadSafeIvoryTower2 =
|
||||
ThreadSafeLazyLoadedIvoryTower.getInstance();
|
||||
var threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower.getInstance();
|
||||
var threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower.getInstance();
|
||||
LOGGER.info("threadSafeIvoryTower1={}", threadSafeIvoryTower1);
|
||||
LOGGER.info("threadSafeIvoryTower2={}", threadSafeIvoryTower2);
|
||||
|
||||
@ -100,11 +97,9 @@ public class App {
|
||||
LOGGER.info(dcl2.toString());
|
||||
|
||||
// initialize on demand holder idiom
|
||||
var demandHolderIdiom =
|
||||
InitializingOnDemandHolderIdiom.getInstance();
|
||||
var demandHolderIdiom = InitializingOnDemandHolderIdiom.getInstance();
|
||||
LOGGER.info(demandHolderIdiom.toString());
|
||||
var demandHolderIdiom2 =
|
||||
InitializingOnDemandHolderIdiom.getInstance();
|
||||
var demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
|
||||
LOGGER.info(demandHolderIdiom2.toString());
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,6 @@ public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ package com.iluwatar.singleton;
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class InitializingOnDemandHolderIdiomTest
|
||||
extends SingletonTest<InitializingOnDemandHolderIdiom> {
|
||||
extends SingletonTest<InitializingOnDemandHolderIdiom> {
|
||||
|
||||
/**
|
||||
* Create a new singleton test instance using the given 'getInstance' method.
|
||||
|
@ -29,13 +29,11 @@ import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeout;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
@ -73,9 +71,9 @@ public abstract class SingletonTest<S> {
|
||||
@Test
|
||||
public void testMultipleCallsReturnTheSameObjectInSameThread() {
|
||||
// Create several instances in the same calling thread
|
||||
S instance1 = this.singletonInstanceMethod.get();
|
||||
S instance2 = this.singletonInstanceMethod.get();
|
||||
S instance3 = this.singletonInstanceMethod.get();
|
||||
var instance1 = this.singletonInstanceMethod.get();
|
||||
var instance2 = this.singletonInstanceMethod.get();
|
||||
var instance3 = this.singletonInstanceMethod.get();
|
||||
// now check they are equal
|
||||
assertSame(instance1, instance2);
|
||||
assertSame(instance1, instance3);
|
||||
@ -89,19 +87,18 @@ public abstract class SingletonTest<S> {
|
||||
public void testMultipleCallsReturnTheSameObjectInDifferentThreads() throws Exception {
|
||||
assertTimeout(ofMillis(10000), () -> {
|
||||
// Create 10000 tasks and inside each callable instantiate the singleton class
|
||||
final List<Callable<S>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
tasks.add(this.singletonInstanceMethod::get);
|
||||
}
|
||||
final var tasks = IntStream.range(0, 10000)
|
||||
.<Callable<S>>mapToObj(i -> this.singletonInstanceMethod::get)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
// Use up to 8 concurrent threads to handle the tasks
|
||||
final ExecutorService executorService = Executors.newFixedThreadPool(8);
|
||||
final List<Future<S>> results = executorService.invokeAll(tasks);
|
||||
final var executorService = Executors.newFixedThreadPool(8);
|
||||
final var results = executorService.invokeAll(tasks);
|
||||
|
||||
// wait for all of the threads to complete
|
||||
final S expectedInstance = this.singletonInstanceMethod.get();
|
||||
for (Future<S> res : results) {
|
||||
final S instance = res.get();
|
||||
final var expectedInstance = this.singletonInstanceMethod.get();
|
||||
for (var res : results) {
|
||||
final var instance = res.get();
|
||||
assertNotNull(instance);
|
||||
assertSame(expectedInstance, instance);
|
||||
}
|
||||
|
@ -23,9 +23,7 @@
|
||||
|
||||
package com.iluwatar.singleton;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
@ -47,11 +45,10 @@ public class ThreadSafeDoubleCheckLockingTest extends SingletonTest<ThreadSafeDo
|
||||
*/
|
||||
@Test(expected = InvocationTargetException.class)
|
||||
public void testCreatingNewInstanceByRefection() throws Exception {
|
||||
ThreadSafeDoubleCheckLocking instance1 = ThreadSafeDoubleCheckLocking.getInstance();
|
||||
Constructor constructor = ThreadSafeDoubleCheckLocking.class.getDeclaredConstructor();
|
||||
ThreadSafeDoubleCheckLocking.getInstance();
|
||||
var constructor = ThreadSafeDoubleCheckLocking.class.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
ThreadSafeDoubleCheckLocking instance2 =
|
||||
(ThreadSafeDoubleCheckLocking) constructor.newInstance(null);
|
||||
constructor.newInstance((Object[]) null);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ package com.iluwatar.singleton;
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class ThreadSafeLazyLoadedIvoryTowerTest
|
||||
extends SingletonTest<ThreadSafeLazyLoadedIvoryTower> {
|
||||
extends SingletonTest<ThreadSafeLazyLoadedIvoryTower> {
|
||||
|
||||
/**
|
||||
* Create a new singleton test instance using the given 'getInstance' method.
|
||||
|
Reference in New Issue
Block a user