Singleton mode
A class has only one instance and can be accessed globally
Application scenarios
Such as account management, database operation, etc. (A certain object is frequently accessed and used)
Common ways
Hungry Man Style
Lazy style
Synchronous locking
DCL double lock verification
Static inner class
Enumerate single cases
Hungry Man Style
The initialization operation is performed immediately while loading the class, which consumes a lot of resources.
public class SingleTest { public static final SingleTest singleTest1=new SingleTest(); public SingleTest() { } public static SingleTest getSingleTest1(){ return singleTest1; } }
Lazy style
Advantages: Initialization and loading are only performed when needed
Disadvantages: Threads are not safe, and it is easy to cause out-of-sync in multithreads.
public class SingleTest1 { private static SingleTest1 instance=null; public SingleTest1() { } public static SingleTest1 getInstance(){ if(instance==null){ instance=new SingleTest1(); } return instance; } }
Synchronous locking
Advantages: Solve thread safety issues
Disadvantages: Each instance needs to determine the locking status, which is inefficient
public class SingleTest2 { private static SingleTest2 instance=null; public SingleTest2() { } public static synchronized SingleTest2 getInstance(){ if(instance==null){ instance=new SingleTest2(); } return instance; } }
DCL double verification
Advantages: It can run perfectly when the concurrency is not high (recommended, there is almost no high concurrency on the client)
Disadvantages: JDK1.5 may have had the problem of instance initialization (now Android is almost 1.7 and 1.8, which can almost skip this problem)
public class SingleTest4 { private static SingleTest4 instance=null;// Static variable public SingleTest4() { } public static SingleTest4 getInstance(){ if (instance == null) {//The first layer of verification synchronized () { if (instance == null) {//The second layer of verification instance = new SingleTest4(); } } } return instance; } }
Static inner class
Advantages: Lazy loading, thread safety, low memory consumption (recommended)
public class SingleTest5 { private SingleTest5() { } public static final SingleTest5 getInstance() { return ; } //Defined static inner class private static class SingletonHolder { private static final SingleTest5 INSTANCE = new SingleTest5(); // Where to create an instance } }
Enumerate single cases
Advantages: thread safety, anti-deserialization, anti-reflection, simple writing
public enum SingleTest6 { //The first method// INSTANCE; // private CaiPiao instance; // // SingleTest6() { // instance = new CaiPiao(); // } // // public CaiPiao getInstance() { // return instance; // } // // class CaiPiao { // // } //The second method INSTANCE2{ @Override protected void CaiPiao() { ("Lottery"); } }; protected abstract void CaiPiao(); }
github code address
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.