I believe everyone is already familiar with the singleton model, and its implementation methods are usually divided into the following two types:
The code copy is as follows:
// Lazy implementation
public class Singleton{
private static Singleton instance = null;
private Singleton(){}
public static newInstance(){
if(null == instance){
instance = new Singleton();
}
return instance;
}
public void doSomething(){
// Do something...
}
}
//Hungry Chinese Style
public class Singleton{
private static Singleton instance = new Singleton();
private Singleton(){}
public static Singleton newInstance(){
return instance;
}
public void doSomething(){
// Do something...
}
}
In the use of single-interest mode, lazy and hungry style have different application scenarios. If creating single-interest object takes up a lot of memory but is not required when starting the application, we generally use lazy style and wait until we really need to use it. Only when the project is started will it be created. If we want to use it immediately as the project is started, we will use the Eagle mode in the initialization to create the Single Benefit object.
In the above two modes, if you are multi-threaded, there will be no problems with the Hungry Man style, because the JVM will only load the single interest class once, but the lazy Man style may have the problem of repeatedly creating single interest objects, which is that the thread does not Safe.
So is there a way to make the single-interest mode of Hungry Man-style thread-safe? There are certainly any answers. People usually use the method of adding synchronization locks to implement it, but this is more troublesome to implement it. We can use the JVM class loading mechanism to implement it. In many cases, the JVM has provided us with synchronization control, such as:
a. Data initialized in static{} block
b. When accessing the final field, etc.
When the JVM is loading the class, it will ensure that the data is synchronized. We can implement it like this:
Use class-level internal classes to create object instances in this internal class. In this way, as long as you do not use class-level internal classes, you will not create object instances, thereby achieving lazy lazy loading and thread safety.
The code copy is as follows:
public class Singleton{
//Inner class, the simple interest object will be created only when the inner class is loaded
private static class SingletonHolder{
public static Singleton instance = new Singleton();
}
private Singleton(){}
public static Singleton newInstance(){
return SingletonHolder.instance;
}
public void doSomething(){
//do something
}
}
This can realize the thread-safe single-price model.
In addition, we can also implement the single-interest mode through enumeration types, which is also a more recommended way.
Implementing a singleton pattern using enum types is as follows:
The code copy is as follows:
public enum Singleton{
//Define an enumerated element, it is an instance of Singleton
instance;
public void doSomething(){
// do something ...
}
}
Ok, the singleton mode is introduced here.