A singleton is also called a single state, which is opposite to polymorphism.
Essence: Each classloader specifies that there is only one instance created by class.
JDK comes with a runtime class, which is a typical Singleton.
Common examples are as follows:
Runtime is a hungry Chinese style.
1. Hungry Chinese Style
Public class Singleton {
Private Static final Singleton instance = new Singleton ();
Private Singleton (){
}
Public static Singleton getinstance (){
Return instance;
}
}
2. Lazy
Public class Singleton {
Private Static Singleton instance = NULL;
Private Singleton (){
}
Public static synchronized Singleton getinstance (){
If (instance = NULL)
Instance = new Singleton ();
Return instance;
}
}
3. DCL
Double-checked locking is a lazy variant mainly to improve the efficiency of lazy locking. It may be faulty when used before jdk1.5 due to the memory model (jmm) at that time.
Because the classCodeDuring Java class name running, JIT is converted to real CPU code for execution to speed up. In this process, the execution sequence of class files may be optimized and merged.
In Windows, you can put the hsdis-i386.dll in jdk1.7.0/JRE/bin/client, and jdk1.7.0/JRE/bin/server, and then enter
Java-XX: + unlockdiagnosticvmoptions-XX: + printassembly myclassname View
In jdk1.5 +, the methods involved in volatile are not optimized and merged, so the execution sequence of // 2 // 3 can be ensured.
Public class Singleton {
Private Static volatile Singleton instance = NULL;
Private Singleton (){
}
Public static Singleton instance () {// 1
If (instance = NULL) {// 2
Synchronized (singleton. Class ){
If (instance = NULL) {// 3
Instance = new Singleton ();
}
}
}
Return instance;
}
}
4. init on demand
To solve the defects of DCl, it can be used in jdk1.2 +, proposed by researcher bill put forward
Public class Singleton {
Private Singleton (){
}
Private Static class singletonholder {
Public static final Singleton instance = new Singleton ();
}
Public static Singleton getinstance (){
Return singletonholder. instance;
}
}
5. Enumeration
The simplest single-State mode, which is strongly recommended by Josh Bloch!
Public Enum Singleton {
Instance;
}
So far, have you finished the singleton discussion?
Ask a question: Will a singleton generate multiple objects ??
In actual work, the class often needs implements serializable? Object serialization and deserialization
Is deserialization an implicit constructor? After deserialization, the singleton fails. How can this problem be solved?
There are two common methods:
1. readresolve
Public class Singleton implements serializable {
Private Static final Singleton instance = new Singleton ();
Private Singleton (){
}
Private object readresolve (){
// Readunshared returns the superficial copy of the object
Return instance;
}
Public static Singleton getinstance (){
Return instance;
}
}
The red code is very important. During deserialization, Ois. readobject (); internal reflection is used to check whether the implements serializable class has
The readresolve method uses the returned value of readresolve as the return value of Ois. readobject ();. Therefore, readresolve must return the reference of the previous object.
Ii. Use Enumeration
The unique enumeration feature is used. The enumerated serialization and deserialization objects are then the same object. You can view the ois. readobject (); OOS. writeobject (OBJ) code on your own. It is
Consider enumeration as a Type separately.