The singleton pattern in Java is a common design pattern, and the following are its features:
- A singleton class can have only one instance.
- The Singleton class must create its own unique instance.
- The Singleton class must provide this instance to all other objects
The first type (lazy, thread insecure):
1 public class Singleton {
2 private static Singleton instance;
3 Private Singleton () {}
4 public static Singleton getinstance () {
5 if (instance = = null) {
6 instance = new Singleton ();
7}
8 return instance;
9}
10}
11
Lazy-type is a typical time to change space , that is, every time you get an instance will be judged to see if you need to create an instance, wasting judgment time. Of course, if no one ever uses it, it doesn't create an instance, saving memory space
Because the lazy implementation is thread-safe, this reduces the speed of the entire visit and is judged every time.
The second type (lazy, thread safe):
1 public class Singleton {
2 private static Singleton instance;
3 Private Singleton () {}
4 public static synchronized Singleton getinstance () {
5 if (instance = = null) {
6 instance = new Singleton ();
7}
8 return instance;
9}
10}
11
The third type (a Hungry man):
1 public class Singleton {
2 private static Singleton instance = new Singleton ();
3 Private Singleton () {}
4 public static Singleton getinstance () {
5 return instance;
6}
7}
8
fourth species (a Hungry man, variant):
1 public class Singleton {
2 private Singleton instance = null;
3 Static {
4 Instance = new Singleton ();
5}
6 private Singleton () {}
7 public static Singleton getinstance () {
8 return this.instance;
9}
10}
11
The surface looks very different, in fact, the third Way is similar, all in class initialization is instantiated instance.
The fifth type (static inner Class):
1 public class Singleton {
2 private static Class Singletonholder {
3 private static final Singleton INSTANCE = new Singleton ();
4}
5 Private Singleton () {}
6 public static final Singleton getinstance () {
7 return singletonholder.instance;
8}
9}
10
Advantage: Static variable instance is not initialized when loading, because there is no active use to reach the lazy loading
The sixth type (enumeration):
1 public enum Singleton {
2 INSTANCE;
3 public void Whatevermethod () {
4}
5}
6
Pros: Not only can avoid multi-threaded synchronization problems, but also to prevent deserialization
Seventh type (double check lock):
1 public class Singleton {
2 private volatile static Singleton Singleton;
3 Private Singleton () {}
4 public static Singleton Getsingleton () {
5 if (singleton = = null) {
6 synchronized (Singleton.class) {
7 if (singleton = = null) {
8 singleton = new Singleton ();
9}
10}
11}
return singleton;
13}
14}
15
Summarize
There are two issues to note:
1. If a single case is loaded by a different class loader, there may be instances of multiple singleton classes. Assumptions are not remote access, such as some
The servlet container for each
The servlet uses a completely different class loader, so that if there are two
The servlet accesses a singleton class, and they all have their own instances.
2. If
Singleton implements the
Java.io.Serializable interface, instances of this class can be serialized and restored. In any case, if you serialize an object of a singleton class and then restore multiple objects, you will have multiple instances of the Singleton class.
The fix for the first problem is:
1 private static Class getclass (String classname)
2 throws ClassNotFoundException {
3 ClassLoader ClassLoader = Thread.CurrentThread (). Getcontextclassloader ();
4
5 if (ClassLoader = = null)
6 ClassLoader = Singleton.class.getClassLoader ();
7
8 Return (Classloader.loadclass (classname));
9}
10}
11
The fix for the second problem is:
1 public class Singleton implements Java.io.Serializable {
2 public static Singleton INSTANCE = new Singleton ();
3
4 protected Singleton () {
5
6}
7 Private Object Readresolve () {
8 return INSTANCE;
9}
10}
11
Singleton (JAVA)