#100 Added test case for demonstrating the thread safety issues of a
naive lazy loaded Singleton implementation.
This commit is contained in:
parent
db6ec3cc1a
commit
96241f63d2
@ -0,0 +1,70 @@
|
||||
package com.iluwatar.singleton;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* This test case demonstrates thread safety issues of lazy loaded Singleton implementation.
|
||||
*
|
||||
* Out of the box you should see the test output something like the following:
|
||||
*
|
||||
* Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
|
||||
* Thread=Thread-2 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
|
||||
* Thread=Thread-0 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
|
||||
* Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
|
||||
* Thread=Thread-3 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
|
||||
* Thread=Thread-1 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@60f330b0
|
||||
* Thread=Thread-2 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e
|
||||
*
|
||||
* By changing the method signature of LazyLoadedIvoryTower#getInstance from
|
||||
* public static LazyLoadedIvoryTower getInstance()
|
||||
* into
|
||||
* public synchronized static LazyLoadedIvoryTower getInstance()
|
||||
* you should see the test output change to something like the following:
|
||||
*
|
||||
* Thread=Thread-4 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
|
||||
* Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
|
||||
* Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
|
||||
* Thread=Thread-3 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
|
||||
* Thread=Thread-2 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
|
||||
* Thread=Thread-1 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490
|
||||
*
|
||||
*/
|
||||
public class LazyLoadedSingletonThreadSafetyTest {
|
||||
|
||||
private static final int NUM_THREADS = 5;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
SingletonThread runnable = new SingletonThread();
|
||||
for (int j=0; j<NUM_THREADS; j++) {
|
||||
Thread thread = new Thread(runnable);
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
private static class SingletonThread implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
LazyLoadedIvoryTower instance = LazyLoadedIvoryTower.getInstance();
|
||||
System.out.println("Thread=" + Thread.currentThread().getName() + " got instance=" + instance);
|
||||
}
|
||||
}
|
||||
|
||||
private static class LazyLoadedIvoryTower {
|
||||
|
||||
private static LazyLoadedIvoryTower instance = null;
|
||||
|
||||
private LazyLoadedIvoryTower() {
|
||||
}
|
||||
|
||||
public static LazyLoadedIvoryTower getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new LazyLoadedIvoryTower();
|
||||
System.out.println("Thread=" + Thread.currentThread().getName() + " creating instance=" + instance);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user