Digression: I want to be strong, to become stronger, stronger, stronger.
1. Singleton application Scenarios and why you should use single
Singleton is an object that can only have one instance in a lifetime. Only one instance can be created by Singleton itself. Outsiders are unable to create instances, and outsiders can only use them.
Because Singleton mode has only one object in life, it can save memory and speed up access. More suitable for use in public places. Like what
1) control of the printer, if each computer can connect multiple printers, there can only be one printerspooler, to avoid the simultaneous output of two print jobs to the printer;
2) Windows Task Manager is a typical singleton mode.
2, the realization principle of Singleton
1, the private construction method
2. Private static reference point to own instance
3. The public static method returns its own instance
3, Singleton realization Way--lazy mode (non-thread-safe)
PackageSS;/*** Non-thread-safe Lazy mode * Advantages: * Avoids the case of creating instances without invoking in the bad-han mode, which improves resource utilization. * Disadvantage: * Non-thread-safe, multiple threads may create multiple instances at the same time, and these instances are not the same object, * Although the instances created later overwrite the previously created instances, there will still be cases where the instances are different. The solution to this problem is to add a sync lock (synchronized). * */ Public classSINGLETON02 {//Private Construction Methods PrivateSingleton02 () {}//an instance for internal invocation Private StaticSingleton02 instance =NULL; //static methods for external access Public StaticSingleton02 getinstance () {if(NULL==instance) {Instance=NewSingleton02 (); } returninstance; }}
4, the singleton realization Way--lazy mode (thread safety)
PackageSS;/*** thread-safe lazy mode (via plus synchronized) * Advantages: * Avoids the possibility of creating multiple instances in SINGLETON02 * * disadvantages * The overhead of synchronization is relatively large, * in fact, only when the first call requires synchronous judgment, other times only need to be sentenced The non-empty return can be. * */ Public classSingleton03 {//Private Construction Methods PrivateSingleton03 () {}//an instance for internal invocation Private StaticSingleton03 instance =NULL; //static methods for external access Public Static synchronizedSingleton03 getinstance () {if(NULL==instance) {Instance=NewSingleton03 (); } returninstance; }}
5, Singleton display mode--a Hungry man mode (thread safety)
PackageSS;/*** A hungry man mode * Advantages: * 1, Thread safety * 2, in the class loading process has created a good instance, loading speed * Disadvantage: * 1, resource efficiency is not high, the instance in some cases after instantiation, may never call to getinstance ()*/ Public classSingleton01 {//Private Construction Methods PrivateSingleton01 () {}//define internal instances for internal calls only Public StaticSingleton01 instance =NewSingleton01 (); //public external calling method Public StaticSingleton01 getinstance () {returninstance; }}
6, the singleton realization Way-double lock (unreliable)
PackageSS;/*** Dual Lock Mode * Origin: * In order to deal with the situation in the thread safety Singleton03, the double lock is born. The goal is to avoid the expensive cost of synchronizing all calls except for the first call. * The cost of synchronization is different between different JVMs. In the early days, the costs were quite high. As more advanced JVMs appear, the cost of synchronization is reduced, but the synchronized method or block still has a performance penalty. * Programmers do not want unnecessary waste of processing time without taking into account the progress of the JVM. * Advantages: * Volatile variables have synchronized visibility characteristics, but do not have atomic properties. This means that threads can automatically discover the latest values of volatile variables. * While volatile use is clearly defined: * The write operation of the variable does not depend on the current value; * The variable is not included in the invariant with other variables; * Volatile is only used if the state is truly independent of other content within the program. * Out-of-order writes to Java memory will cause problems with double locking failures. * * How to solve the reference blog: *http://blog.csdn.net/chenchaofuck1/article/details/51702129 * */ Public classSingleton04 {//Private Constructors PrivateSingleton04 () {}//instances of internal access Private Static volatileSingleton04 instance =NULL; //static classes for external access Public StaticSingleton04 getinstance () {if(NULL==instance) { synchronized(Singleton04.class){ if(NULL==instance) {Instance=NewSingleton04 (); } } } returninstance; } /*////The following code will show the situation,//When instance is null, two threads enter the inside of the IF statement concurrently. Then, a thread enters the synchronized block to initialize the instance,//and the other thread is blocked. When a thread exits the synchronized block, the waiting thread enters and creates another singleton object//In this case, there is a "double check lock" public static Singleton04 getinstance () { if (null = = instance) {synchronized (singleton04.class) {instance = new Singleton04 (); }} return instance; } */}
Singleton principle and implementation of Java design pattern