From 96241f63d2a28449d36f7a4d2cf502d0786caf8d Mon Sep 17 00:00:00 2001 From: Ilkka Seppala Date: Thu, 9 Jul 2015 10:36:33 +0300 Subject: [PATCH] #100 Added test case for demonstrating the thread safety issues of a naive lazy loaded Singleton implementation. --- .../LazyLoadedSingletonThreadSafetyTest.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java diff --git a/singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java b/singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java new file mode 100644 index 000000000..f40d8a319 --- /dev/null +++ b/singleton/src/test/java/com/iluwatar/singleton/LazyLoadedSingletonThreadSafetyTest.java @@ -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