The singleton pattern is a cliche , Here is a summary of the implementation of the singleton pattern .
classification
* Lazy man model
* The model of the hungry man
* IoDH technology - The combination of lazy man mode and hungry man mode
Strengths and weaknesses
* Lazy man model
* The model of the hungry man
* IoDH technology - The combination of lazy man mode and hungry man mode
classification
Lazy man model
Lazy mode is a technique that does not load until the singleton pattern needs to be used . An important point of singleton pattern , To ensure the correctness of multithreading operation . to this end , There are many ways to ensure thread safety in different lazy man modes .
synchronized It can ensure the thread safety of the method , The specific method is to use sychronized Methods of modification and sychronized
There are two ways to modify the code block .
synchronized Method modified singleton model
package mode; /** * Created by szh on 2020/6/28. * * @author szh */ public
class SingletonOne { private static SingletonOne singletonOne = null; private
SingletonOne() { } public synchronized static SingletonOne getInstance(){
if(singletonOne == null){ singletonOne = new SingletonOne(); } return
singletonOne; } public static void main(String[] args) {
SingletonOne.getInstance(); System.out.println("------------------------"); } }
There are some problems with the above code , Every call getInstance() The thread locking judgment is needed , In multithreading and high concurrency access environment , The system performance will be greatly reduced .
synchronized Singleton pattern of code block modification
in the light of synchronized Modification method , It can reduce the granularity of the lock , Reduce to code block level , Here is the code :
package mode; /** * Created by szh on 2020/6/28. * * @author szh */ public
class SingletonTwo { private static volatile SingletonTwo singletonTwo = null;
private SingletonTwo() { System.out.println("init"); } public static
SingletonTwo getInstance() { if (singletonTwo == null) { synchronized
(SingletonTwo.class) { if (singletonTwo == null) { singletonTwo = new
SingletonTwo(); } } } return singletonTwo; } public static void main(String[]
args) { SingletonTwo.getInstance();
System.out.println("======================="); } }
* volatile Modifying static member variables
There's a little bit of attention , Need to use volatile modification Static member variable , Double check failure caused by code optimization . But it also needs attention ,volatile Keywords will be masked
Java Some code optimization of virtual machine lock , It may lead to low system operation efficiency . therefore , Using double check locks to implement singleton mode is not a perfect way .
The model of the hungry man
The model of the hungry man , You can ensure that the class instantiates itself when it is loaded , You don't have to think about multithreading .
Here is the code
package mode; /** * Created by szh on 2020/6/29. */ public class
SingletonThree { private static SingletonThree singletonThree = new
SingletonThree(); private SingletonThree(){ System.out.println("init"); }
public static SingletonThree getInstance(){ return singletonThree; } public
static void main(String[] args){ SingletonThree.getInstance();
System.out.println("-----------------------------"); } }
There are also some problems in the model of starvation , The above code solves the problem of thread safety , But every time you call getInstance()
All the time, we need to make thread locking judgment , In multithreading and high concurrency access environment , The system performance will be greatly reduced .
IoDH technology - The combination of lazy man mode and hungry man mode
package mode; /** * Created by szh on 2020/6/29. */ public class SingletonFour
{ private SingletonFour(){ System.out.println("init"); } private static class
SingletonFourInner{ private final static SingletonFour instance = new
SingletonFour(); } private static SingletonFour getInstance(){ return
SingletonFourInner.instance; } public static void main(String[] args){
SingletonFour.getInstance();
System.out.println("----------------------------"); } }
analysis :
Static singleton objects do not act as Singleton Directly instantiate member variables of , Therefore, the class is not instantiated when it is loaded Sinleton.
Only the first call getInstance() The inner class is loaded when , from Java Virtual machine ensures thread safety , Ensure that the member variable can only be initialized once .
Strengths and weaknesses
*
Lazy man model
advantage : Class is loaded only when it is used
inferiority :1) Complex code is needed to ensure thread safety
2) Even if thread safety is ensured , Efficiency is not the highest
*
The model of the hungry man
advantage : The code is simple , There is no need to consider multithreading security
inferiority : Generate instances when a class is not in use , Worst performance
*
IoDH technology - The combination of lazy man mode and hungry man mode
advantage : Highest performance
inferiority : It has language characteristics ,Java Other languages are not supported
Technology
Daily Recommendation