Singleton mode
The main points of the singleton pattern
Singleton mode belongs to creation mode
Three main points:
- There can only be one instance of a class
- It must create this instance by itself
- It must provide this instance to the entire system on its own
Features of singleton mode
- A singleton class can only have one instance
- Singleton class must create its own unique instance
- Singleton class must provide this instance to all other objects
Hungry Chinese Singleton
Thread safe, easy to implement.
Advantages: no locks, high execution efficiency.
Disadvantages: the class is initialized when it is loaded, which wastes memory
public class Singleton {
private final static Singleton SINGLETON = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return SINGLETON;
}
}
Another implementation:
public class Singleton {
private static Singleton instance;
static {
instance = new Singleton();
}
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}
Lazy singleton
One: Lazy man, thread is not safe
Simple to implement,
no locks, unsafe in multi-threaded situations
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Two: lazy man, thread safe
Simple to implement and thread safe.
Advantages: Initialize only when it is called for the first time, avoiding memory consumption.
Disadvantages: The synchronized keyword is added, which is inefficient.
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Three: Lazy style, thread is not safe
Simple to implement, unsafe to make
a singleton
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
instance = new Singleton();
}
}
return instance;
}
}
Four: Lazy style, double check lock/double check lock
Since JDK1.5,
thread safety, implementation is more complicated.
Advantages: thread safety, lazy loading, higher efficiency.
Note: volatile keyword modification is critical
public class Singleton {
private static volatile Singleton instance;
private Singleton () {
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
Five: Lazy, static inner class
Advantages: thread safety, lazy loading, high efficiency
Principle: The static properties of the class will only be initialized when the class is loaded for the first time. Here, JVM helps us to ensure the safety of threads. When the class is initialized, other threads cannot enter.
public class Singleton {
private Singleton() {
}
private static class LazySingletonInstance {
private static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return LazySingletonInstance.instance;
}
}
Six: lazy man, enumeration
Since JDK1.5,
thread safety and simple implementation
Advantages: more concise, automatic serialization mechanism is supported, and multiple instantiations are absolutely prevented
public enum Singleton {
INSTANCE;
public void whateverMethod() {
}
}
to sum up
Generally speaking, it is not recommended to use the first, second, and third methods. It is recommended to use the hungry man method. The fifth static inner class method is only used when the lazy loading effect is to be clearly realized. If it involves deserialization to create an object, you can try the sixth enumeration method. If you have other special needs, you can consider using the fourth double check lock method