The first (lazy man, the thread is not safe):
Public class singleton {Private Static Singleton Instance; Private Singlet () {} Public Static Singleton GetinStance () {INSTANCE == NULL) {Instance = New Sing. Leton ();} Return Instance;}}
This way of writing is obvious, but the fatal is that it cannot work normally on multi -threaded.
The second type (lazy man, thread safety):
Public class singleton {Private Static Singlet Instance; Private Singlet () {} Public Static Synchronized Singletonstance () {INSTANCE == NULL) {Inst. ANCE = New Singleton ();} Return Instance;}}
This way of writing can work well in multi -threaded, and it seems that it also has a good Lazy Loading. However, unfortunately, the efficiency is very low, and 99%does not need to be synchronized.
Third (hungry man):
Public class singleton {Private Static Singlet Instance = New Singlet (); Private Singlet () {} Public Static Singleton GetInstance () {Return Instance;}}}
This method is based on the ClassLoder mechanism to avoid multi -threaded synchronization problems. However It cannot be determined that there are other methods (or other static methods) cause class loading. At this time, initialization Instance obviously did not achieve the effect of lazy loading.
The fourth type (hungry man, variant):
Public class singleton {Private Singlet Instance = NULL; Static {Instance = New Singlet (); turn this.instance;}}
On the surface, it looks very different. In fact, the third way is similar. They are initialized in class, that is, instantiated Instance.
Fifth (static internal class):
Public Class Singleton {Private Static Class Singleter {Private Static Final Singlet Instance = New Singleton ();} Private Singlet () {} Public Static l Singleton Getinstance () {Return Singletonholder.instance;}}
This method also uses the ClassLoder's mechanism to ensure that there is only one thread when initialized Instance. It is different from the third and fourth ways (very small differences): the third and fourth ways are as long as the Singleton class is only Singleton class When loaded, then Instance will be instantiated (did not achieve the lazy loading effect), and this method is that the Singleton class is loaded, and Instance may not be initialized. Because the Singletonholder class is not actively used, it will only display the Singletonholder class when it is displayed by calling the GetInstance method to instantly instantiated Instance. Imagine that if the institutionalized Instance consumes resources, I want him to delay loading. On the other hand, I don't want to be instantiated when loading the Singleton class, because I cannot ensure that the Singleton class may be actively used in other places to use it to so as to be used as a result. It is loaded, then instantiated Instance is obviously inappropriate. At this time, this method is very reasonable compared to the third and fourth ways.
The sixth type (enumeration):
public enum singleton {instance; public void waterMethod () {}}
This method is an Effective Java author Josh Block. I just joined the enum characteristics. I can't help but make people feel rusty in this way. In actual work, I have rarely seen someone who wrote it like this.
Seventh (double school lock):
Public Class Singleton {Private Volatile Static Singleton Singleton; Private Singleton () {} Public Static Singletonleton () {IF (SINGLEN == NULL) izlet (Singleton.class) {if (Singleton == NULL) {Singleton = New Singleton ();}}} Return Singleton;}}
This is an upgraded version of the second way, commonly known as double inspection lock, please check in detail: http://www.ibm.com/developerworks/cn/java/j-dCl.html
After JDK1.5, the double inspection lock can achieve the singular effect normally.
Summarize
There are two questions to pay attention to:
1. If the single case is loaded by different types of loaders, there may be multiple single -case instances. Suppose it is not a remote access. For example, some Servlet containers use completely different types of loaders for each Servlet. In this way, if there are two Servlet accessing a single class, they will have their own examples.
2. If Singleton implements java.io.serializable interface, then instances of this class may be serialized and restored. In any case, if you serialize an object of a singles class, and then restore multiple objects, then you will have multiple examples of single -case class.
The way to repair the first question is:
Private Static Class Getclass (String ClassName)
Throws ClassNotfoundException {
Classloader classloader = thread.currentthRead (). GetContextClassloader ();
if (ClassLoader == NULL)
classloader = singleton.class.getClassLoader ();
Return (Classloader.loadClass (ClassName));
}
}
The method to repair the second question is
Public Class Singletton Implements java.io.serializable {
public static singleton instance = new singleton ();
Protected Singleton () {
}
Private object readResolve () {
Return Instance;
}
}
For me, I prefer the third and fifth way, simple and easy to understand, and realize thread safety in the JVM layer (if it is not a multi -class loader environment). In general, I will use the third The way method will use the fifth method only when the lazy loading effect is clearly achieved. In addition, if it involves the counter -order creation object, I will try to use the enumeration method to achieve a single example. Ensure that my program is safe, and I will never use the first and second methods. If there are other special needs, I may use the seventh method. After all, JDK1.5 has no double inspection locking locks. The problem is.
=========================================== =====================
SuperHeizai classmates summarized very well:
However, in general, the first one is not a single example, the fourth and third type are one. If calculated, the fifth type can also be written separately. Therefore, the general singles are five ways of writing. Lazy men, evil men, dual school locks, enumeration and static internal classes.
I am very happy to have such a reader and encourage them together.