Skip to content

Commit 012da33

Browse files
committed
add double check lock Singleton
1 parent 399a1bd commit 012da33

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class DoubleCheckLockSingleton
2+
{
3+
private static DoubleCheckLockSingleton instance = null;
4+
5+
// Make this instance can be accessed only by public method
6+
private DoubleCheckLockSingleton()
7+
{
8+
}
9+
10+
public static DoubleCheckLockSingleton getInstance()
11+
{
12+
// Only if it's the first for creating instance
13+
if (instance == null)
14+
{
15+
// The method is synchronized on the class object
16+
synchronized(DoubleCheckLockSingleton.class)
17+
{
18+
if(instance == null)
19+
{
20+
instance = new DoubleCheckLockSingleton();
21+
}
22+
}
23+
}
24+
return instance;
25+
}
26+
}

0 commit comments

Comments
 (0)