|
2 | 2 |
|
3 | 3 | import org.junit.Test; |
4 | 4 |
|
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.List; |
| 8 | +import java.util.concurrent.*; |
| 9 | + |
| 10 | +import static org.junit.Assert.assertEquals; |
| 11 | + |
5 | 12 | /** |
6 | | - * |
7 | | - * This test case demonstrates thread safety issues of lazy loaded Singleton implementation. |
8 | | - * <p> |
9 | | - * Out of the box you should see the test output something like the following: |
10 | | - * <p> |
11 | | - * Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e |
12 | | - * Thread=Thread-2 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e |
13 | | - * Thread=Thread-0 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e |
14 | | - * Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e |
15 | | - * Thread=Thread-3 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e |
16 | | - * Thread=Thread-1 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@60f330b0 |
17 | | - * Thread=Thread-2 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@6fde356e |
18 | | - * <p> |
19 | | - * By changing the method signature of LazyLoadedIvoryTower#getInstance from |
20 | | - * <p><blockquote><pre> |
21 | | - * public static LazyLoadedIvoryTower getInstance() |
22 | | - * </pre></blockquote><p> |
23 | | - * into |
24 | | - * <p><blockquote><pre> |
25 | | - * public synchronized static LazyLoadedIvoryTower getInstance() |
26 | | - * </pre></blockquote><p> |
27 | | - * you should see the test output change to something like the following: |
28 | | - * <p> |
29 | | - * Thread=Thread-4 creating instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490 |
30 | | - * Thread=Thread-4 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490 |
31 | | - * Thread=Thread-0 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490 |
32 | | - * Thread=Thread-3 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490 |
33 | | - * Thread=Thread-2 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490 |
34 | | - * Thread=Thread-1 got instance=com.iluwatar.singleton.LazyLoadedSingletonThreadSafetyTest$LazyLoadedIvoryTower@3c688490 |
| 13 | + * This class provides several test case that test singleton construction. |
| 14 | + * |
| 15 | + * The first proves that multiple calls to the singleton getInstance object are the same when called in the SAME thread. |
| 16 | + * The second proves that multiple calls to the singleton getInstance object are the same when called in the DIFFERENT thread. |
35 | 17 | * |
36 | 18 | */ |
37 | 19 | public class LazyLoadedSingletonThreadSafetyTest { |
38 | 20 |
|
39 | | - private static final int NUM_THREADS = 5; |
40 | | - |
41 | | - @Test |
42 | | - public void test() { |
43 | | - SingletonThread runnable = new SingletonThread(); |
44 | | - for (int j=0; j<NUM_THREADS; j++) { |
45 | | - Thread thread = new Thread(runnable); |
46 | | - thread.start(); |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - private static class SingletonThread implements Runnable { |
| 21 | + private static final int NUM_THREADS = 5; |
| 22 | + private List<ThreadSafeLazyLoadedIvoryTower> threadObjects = Collections.synchronizedList(new ArrayList<>()); |
| 23 | + |
| 24 | + //NullObject class so Callable has to return something |
| 25 | + private class NullObject{private NullObject(){}} |
| 26 | + |
| 27 | + @Test |
| 28 | + public void test_MultipleCallsReturnTheSameObjectInSameThread() { |
| 29 | + //Create several instances in the same calling thread |
| 30 | + ThreadSafeLazyLoadedIvoryTower instance1 = ThreadSafeLazyLoadedIvoryTower.getInstance(); |
| 31 | + ThreadSafeLazyLoadedIvoryTower instance2 = ThreadSafeLazyLoadedIvoryTower.getInstance(); |
| 32 | + ThreadSafeLazyLoadedIvoryTower instance3 = ThreadSafeLazyLoadedIvoryTower.getInstance(); |
| 33 | + //now check they are equal |
| 34 | + assertEquals(instance1, instance1); |
| 35 | + assertEquals(instance1, instance2); |
| 36 | + assertEquals(instance2, instance3); |
| 37 | + assertEquals(instance1, instance3); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void test_MultipleCallsReturnTheSameObjectInDifferentThreads() throws InterruptedException, ExecutionException { |
| 42 | + {//create several threads and inside each callable instantiate the singleton class |
| 43 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 44 | + |
| 45 | + List<Callable<NullObject>> threadList = new ArrayList<>(); |
| 46 | + for (int i = 0; i < NUM_THREADS; i++) { |
| 47 | + threadList.add(new SingletonCreatingThread()); |
| 48 | + } |
| 49 | + |
| 50 | + ExecutorService service = Executors.newCachedThreadPool(); |
| 51 | + List<Future<NullObject>> results = service.invokeAll(threadList); |
51 | 52 |
|
52 | | - @Override |
53 | | - public void run() { |
54 | | - LazyLoadedIvoryTower instance = LazyLoadedIvoryTower.getInstance(); |
55 | | - System.out.println("Thread=" + Thread.currentThread().getName() + " got instance=" + instance); |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - private static class LazyLoadedIvoryTower { |
| 53 | + //wait for all of the threads to complete |
| 54 | + for (Future res : results) { |
| 55 | + res.get(); |
| 56 | + } |
60 | 57 |
|
61 | | - private static LazyLoadedIvoryTower instance = null; |
62 | | - |
63 | | - private LazyLoadedIvoryTower() { |
64 | | - } |
| 58 | + //tidy up the executor |
| 59 | + executorService.shutdown(); |
| 60 | + } |
| 61 | + {//now check the contents that were added to threadObjects by each thread |
| 62 | + assertEquals(NUM_THREADS, threadObjects.size()); |
| 63 | + assertEquals(threadObjects.get(0), threadObjects.get(1)); |
| 64 | + assertEquals(threadObjects.get(1), threadObjects.get(2)); |
| 65 | + assertEquals(threadObjects.get(2), threadObjects.get(3)); |
| 66 | + assertEquals(threadObjects.get(3), threadObjects.get(4)); |
| 67 | + } |
| 68 | + } |
65 | 69 |
|
66 | | - public static LazyLoadedIvoryTower getInstance() { |
67 | | - if (instance == null) { |
68 | | - instance = new LazyLoadedIvoryTower(); |
69 | | - System.out.println("Thread=" + Thread.currentThread().getName() + " creating instance=" + instance); |
70 | | - } |
71 | | - return instance; |
72 | | - } |
73 | | - } |
| 70 | + private class SingletonCreatingThread implements Callable<NullObject> { |
| 71 | + @Override |
| 72 | + public NullObject call() { |
| 73 | + //instantiate the thread safety class and add to list to test afterwards |
| 74 | + ThreadSafeLazyLoadedIvoryTower instance = ThreadSafeLazyLoadedIvoryTower.getInstance(); |
| 75 | + threadObjects.add(instance); |
| 76 | + return new NullObject();//return null object (cannot return Void) |
| 77 | + } |
| 78 | + } |
74 | 79 | } |
0 commit comments