Learn to Organize
Full-han mode (lazy mode)
1 //Full Han2 //Unthreadsafe3 Public classSingleton1 {4 Private StaticSingleton1 Singleton =NULL;5 PrivateSingleton1 () {6 }7 Public StaticSingleton1 getinstance () {8 if(Singleton = =NULL) {9Singleton =NewSingleton1 ();Ten } One returnSingleton; A } -}
Advantages: Lazy Loading start fast, resource consumption is small, use is only instantiated, no lock.
Cons: Non-thread safe.
Full-han mode (lazy mode)-thread safety
1 Public classSingleton {2 3 /**4 * Define a variable to store the created class instance5 */6 7 Private StaticSingleton uniqueinstance =NULL;8 9 /**Ten * Privatization of construction methods, fortunately, the number of instances created by internal control One */ A - PrivateSingleton () { - } the - /** - * Define a method to provide a class instance to the client - * @returnAn example of a singleton + */ - + Public Static synchronizedSingleton getinstance () { A at //determine if the variable for the stored instance has a value - if(Uniqueinstance = =NULL){ - //if not, create an instance of the class and assign the value to the variable of the storage class instance -Uniqueinstance =NewSingleton (); - } - in //If there is a value, then use it directly - returnuniqueinstance; to } + - /** the * Schematic method, single case can have its own operation * */ $ Panax Notoginseng Public voidsingletonoperation () { - the //function Handling + A } the + /** - * Schematic attributes, Singleton can have its own properties $ */ $ - PrivateString Singletondata; - the /** - * Schematic method to allow external access to the value of the property through these methodsWuyi * @returnthe value of the property the */ - Wu PublicString Getsingletondata () { - About returnSingletondata; $ - } - -}
Pros: Ditto, but locked.
Disadvantage: Synchronized is an exclusive exclusive lock with poor concurrency performance. Even after a successful creation, the get instance is still serialized.
Full-han mode (lazy mode)-double lock check DCL (double check Lock)
1 Public classSingleton {2 3 /**4 * Add a volatile modifier to a variable that holds an instance5 */6 7 Private volatile StaticSingleton instance =NULL;8 9 PrivateSingleton () {Ten One } A - Public StaticSingleton getinstance () { - the //Check that the instance exists before entering the following synchronization block if it does not exist - - if(Instance = =NULL){ - + //synchronization blocks, thread-safe creation instances - + synchronized(Singleton.class){ A at //Check to see if the instance exists and create the instance if it does not exist - - if(Instance = =NULL){ - -Instance =NewSingleton (); - in } - to } + - } the * returninstance; $ Panax Notoginseng } - the}
Pros: Lazy loading, thread safety.
Note: the instance must have a volatile keyword decoration, which guarantees that initialization is complete.
A Hungry man mode
1 Public classSingleton {2 3 //4: Define a static variable to store the created class instance4 5 //create the class instance directly here and create it only once6 7 Private StaticSingleton instance =NewSingleton ();8 9 //1: Privatization construction method, good for internal control number of instances createdTen One PrivateSingleton () { A - } - the //2: Define a method to provide a class instance to the client - - //3: This method needs to be defined as a class method, that is, to add static - + //There's no need to control the code in this way. - + Public StaticSingleton getinstance () { A at //5: Direct use of already created instances - - returninstance; - - } - in}
Pros: A hungry man mode is inherently thread-safe and has no latency when used.
Disadvantage: Create an instance at startup, slow startup, and may cause resource waste.
Holder mode
1 Public classSingleton {2 /**3 * Class-level inner class, which is a static member-inner class, an instance of the inner class and an instance of the outer class4 * There is no binding relationship, and only the call is loaded, which allows for lazy loading5 */6 Private Static classsingletonholder{7 /**8 * Static initializers, which are guaranteed by the JVM for thread safety9 */Ten Private StaticSingleton instance =NewSingleton (); One } A /** - * Privatization Structuring Method - */ the PrivateSingleton () { - } - Public StaticSingleton getinstance () { - returnsingletonholder.instance; + } -}
Pros: A way to combine lazy loading with thread safety (no locks). Recommended
Note:
1. Global sharing, one share;
2. The constructor is not exposed (if the exposure can not guarantee a copy), the self-responsibility of their own structure;
3. Lazy type: Lazy load, use to load, non-thread-safe. How to ensure thread safety:
(1) Synchronized getinstance ().
(2) Double check Locking (volatile).
4. A hungry man: The application is good from the start, a waste of resources, but its thread-safe.
5. Holder mode:
(1) Change to internal class, which is guaranteed by the JVM for thread safety.
Java Singleton Mode 5 notation