-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode to written
More file actions
42 lines (29 loc) · 715 Bytes
/
Copy pathcode to written
File metadata and controls
42 lines (29 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
singleton thread safe
//lazy initialzation
public class GreatSingleton{
private static final instance;
private GreatSingleton(){
}
public static synchronized GreatSingleton getInstance(){
if(instance == null){
instance=new GreatSingleton();
}
return instance;
}
}
//double checked
public class GreatSingleton{
private static volatile GreatSingleton instance;
private GreatSingleton()
}
public static GreatSingleton getInstance(){
if(instance == null){ // first check (no locking )
synchronized(GreatSingleton.class){
if(instance == null){ // secound check (locked)
instance=new GreatSingleton();
}
}
}
return instance;
}
}