Skip to content

Commit 5f83980

Browse files
auraspherepivovarit
authored andcommitted
BAEL 1639: added singleton examples and test. (eugenp#3977)
1 parent 5b53866 commit 5f83980

6 files changed

Lines changed: 279 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.designpatterns.singleton.synchronization;
2+
3+
/**
4+
* Double-checked locking design pattern applied to a singleton.
5+
*
6+
* @author Donato Rimenti
7+
*
8+
*/
9+
public class DclSingleton {
10+
11+
/**
12+
* Current instance of the singleton.
13+
*/
14+
private static volatile DclSingleton instance;
15+
16+
/**
17+
* Private constructor to avoid instantiation.
18+
*/
19+
private DclSingleton() {
20+
}
21+
22+
/**
23+
* Returns the current instance of the singleton.
24+
*
25+
* @return the current instance of the singleton
26+
*/
27+
public static DclSingleton getInstance() {
28+
if (instance == null) {
29+
synchronized (DclSingleton.class) {
30+
if (instance == null) {
31+
instance = new DclSingleton();
32+
}
33+
}
34+
}
35+
return instance;
36+
}
37+
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.designpatterns.singleton.synchronization;
2+
3+
/**
4+
* Draconian singleton. The method to get the instance is synchronized.
5+
*
6+
* @author Donato Rimenti
7+
*
8+
*/
9+
public class DraconianSingleton {
10+
11+
/**
12+
* Current instance of the singleton.
13+
*/
14+
private static DraconianSingleton instance;
15+
16+
/**
17+
* Private constructor to avoid instantiation.
18+
*/
19+
private DraconianSingleton() {
20+
}
21+
22+
/**
23+
* Returns the current instance of the singleton.
24+
*
25+
* @return the current instance of the singleton
26+
*/
27+
public static synchronized DraconianSingleton getInstance() {
28+
if (instance == null) {
29+
instance = new DraconianSingleton();
30+
}
31+
return instance;
32+
}
33+
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.designpatterns.singleton.synchronization;
2+
3+
/**
4+
* Singleton with early initialization. Inlines the singleton instance
5+
* initialization.
6+
*
7+
* @author Donato Rimenti
8+
*
9+
*/
10+
public class EarlyInitSingleton {
11+
12+
/**
13+
* Current instance of the singleton.
14+
*/
15+
private static final EarlyInitSingleton INSTANCE = new EarlyInitSingleton();
16+
17+
/**
18+
* Private constructor to avoid instantiation.
19+
*/
20+
private EarlyInitSingleton() {
21+
}
22+
23+
/**
24+
* Returns the current instance of the singleton.
25+
*
26+
* @return the current instance of the singleton
27+
*/
28+
public static EarlyInitSingleton getInstance() {
29+
return INSTANCE;
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.designpatterns.singleton.synchronization;
2+
3+
/**
4+
* Enum singleton pattern. Uses an enum to hold a reference to the singleton
5+
* instance.
6+
*
7+
* @author Donato Rimenti
8+
*
9+
*/
10+
public enum EnumSingleton {
11+
12+
/**
13+
* Current instance of the singleton.
14+
*/
15+
INSTANCE;
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.designpatterns.singleton.synchronization;
2+
3+
/**
4+
* Initialization on demand singleton pattern. Uses a nested static class to
5+
* hold a reference to the singleton instance.
6+
*
7+
* @author Donato Rimenti
8+
*
9+
*/
10+
public class InitOnDemandSingleton {
11+
12+
/**
13+
* Holder for a singleton instance.
14+
*
15+
* @author Donato Rimenti
16+
*
17+
*/
18+
private static class InstanceHolder {
19+
20+
/**
21+
* Current instance of the singleton.
22+
*/
23+
private static final InitOnDemandSingleton INSTANCE = new InitOnDemandSingleton();
24+
}
25+
26+
/**
27+
* Private constructor to avoid instantiation.
28+
*/
29+
private InitOnDemandSingleton() {
30+
}
31+
32+
/**
33+
* Returns the current instance of the singleton.
34+
*
35+
* @return the current instance of the singleton
36+
*/
37+
public static InitOnDemandSingleton getInstance() {
38+
return InstanceHolder.INSTANCE;
39+
}
40+
41+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.baeldung.designpatterns.singleton.synchronization;
2+
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
/**
13+
* Unit tests for the singleton synchronization package with the same name.
14+
*
15+
* @author Donato Rimenti
16+
*
17+
*/
18+
public class SingletonSynchronizationUnitTest {
19+
20+
/**
21+
* Size of the thread pools used.
22+
*/
23+
private static final int POOL_SIZE = 1_000;
24+
25+
/**
26+
* Number of tasks to submit.
27+
*/
28+
private static final int TASKS_TO_SUBMIT = 1_000_000;
29+
30+
/**
31+
* Tests the thread-safety of {@link DraconianSingleton}.
32+
*/
33+
@Test
34+
public void givenDraconianSingleton_whenMultithreadInstancesEquals_thenTrue() {
35+
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
36+
Set<DraconianSingleton> resultSet = Collections.synchronizedSet(new HashSet<DraconianSingleton>());
37+
38+
// Submits the instantiation tasks.
39+
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
40+
executor.submit(() -> resultSet.add(DraconianSingleton.getInstance()));
41+
}
42+
43+
// Since the instance of the object we inserted into the set is always
44+
// the same, the size should be one.
45+
Assert.assertEquals(1, resultSet.size());
46+
}
47+
48+
/**
49+
* Tests the thread-safety of {@link DclSingleton}.
50+
*/
51+
@Test
52+
public void givenDclSingleton_whenMultithreadInstancesEquals_thenTrue() {
53+
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
54+
Set<DclSingleton> resultSet = Collections.synchronizedSet(new HashSet<DclSingleton>());
55+
56+
// Submits the instantiation tasks.
57+
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
58+
executor.submit(() -> resultSet.add(DclSingleton.getInstance()));
59+
}
60+
61+
// Since the instance of the object we inserted into the set is always
62+
// the same, the size should be one.
63+
Assert.assertEquals(1, resultSet.size());
64+
}
65+
66+
/**
67+
* Tests the thread-safety of {@link EarlyInitSingleton}.
68+
*/
69+
@Test
70+
public void givenEarlyInitSingleton_whenMultithreadInstancesEquals_thenTrue() {
71+
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
72+
Set<EarlyInitSingleton> resultSet = Collections.synchronizedSet(new HashSet<EarlyInitSingleton>());
73+
74+
// Submits the instantiation tasks.
75+
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
76+
executor.submit(() -> resultSet.add(EarlyInitSingleton.getInstance()));
77+
}
78+
79+
// Since the instance of the object we inserted into the set is always
80+
// the same, the size should be one.
81+
Assert.assertEquals(1, resultSet.size());
82+
}
83+
84+
/**
85+
* Tests the thread-safety of {@link InitOnDemandSingleton}.
86+
*/
87+
@Test
88+
public void givenInitOnDemandSingleton_whenMultithreadInstancesEquals_thenTrue() {
89+
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
90+
Set<InitOnDemandSingleton> resultSet = Collections.synchronizedSet(new HashSet<InitOnDemandSingleton>());
91+
92+
// Submits the instantiation tasks.
93+
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
94+
executor.submit(() -> resultSet.add(InitOnDemandSingleton.getInstance()));
95+
}
96+
97+
// Since the instance of the object we inserted into the set is always
98+
// the same, the size should be one.
99+
Assert.assertEquals(1, resultSet.size());
100+
}
101+
102+
/**
103+
* Tests the thread-safety of {@link EnumSingleton}.
104+
*/
105+
@Test
106+
public void givenEnumSingleton_whenMultithreadInstancesEquals_thenTrue() {
107+
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
108+
Set<EnumSingleton> resultSet = Collections.synchronizedSet(new HashSet<EnumSingleton>());
109+
110+
// Submits the instantiation tasks.
111+
for (int i = 0; i < TASKS_TO_SUBMIT; i++) {
112+
executor.submit(() -> resultSet.add(EnumSingleton.INSTANCE));
113+
}
114+
115+
// Since the instance of the object we inserted into the set is always
116+
// the same, the size should be one.
117+
Assert.assertEquals(1, resultSet.size());
118+
}
119+
}

0 commit comments

Comments
 (0)