In Single Pattern, only one instance of an object can be represented by a singleton. The singleton pattern is implemented in such a way that a class can only return a reference to an object and a method that gets the unique instance (this method must be a static method).
Through the singleton mode, you can ensure that there is only one instance in the system, thus saving or controlling system resources in certain specific situations.
Create an instance from the beginning. Although relatively simple and common. But one obvious drawback is that an instance is created whether or not the method that gets the instance is called.
public class Single {
private static final Single single = new Single();
private Single(){}
public static Single getSingle(){
return single;
}
}
He improved the inadequacies of the above "hungry mode" and calledgetSingle() The method first determines whether the instance is empty, creates an instance if it is empty, and creates it when the method is called.
public class Single {
private static Single single;
private Single(){}
public static Single getSingle(){
if (single == null){
single = new Single();
}
return single;
}
}
Lazy modeThere are also problems. That is if there are multiple threads calling in parallelgetSingle() At the time of the method, multiple instances will still be created, and the single instance mode will be invalid. So set up thread synchronization on this basis (keysynchonized)。synchronizedThe role is to ensure that at most one thread is running at the same time, thus avoiding the problems caused by multi-threading.
public class Single {
private static Single single;
private Single(){}
public static synchronized Single getSingle(){
if (single == null){
single = new Single();
}
return single;
}
}
The above solution is not perfect. Every callgetSingle()Thread synchronization is required, consider this situation,singleThe object has been created, it can be returned directly to the object, but it has to be synchronized.
So on this basis, there isDouble check lockMethods
public class Single {
private volatile static Single single;
private Single(){}
public static Single getSingle(){
if (single == null){
synchronized (Single.class){
if(single == null){
single = new Single();
}
}
}
return single;
}
}
Also need to add one to the instancevolatile The keyword, its role is to prevent the compiler to optimize the code itself, to ensure that the order of the code is fixed. This involvesJVMThe code optimization mechanism.
The above methods are constantly improving, a bit complicated, but they can passStatic inner classTo ensure thread safety.
public class Single {
private static class SingleHoder{
private static final Single single = new Single();
}
private Single(){}
public static Single getSingle(){
return SingleHoder.single;
}
}
Single classIs private, exceptgetSingle() methodThere is no other way to access the instance, and the instance object will only be created when the method is called.
This method is the simplest. We can passSingle.INSANCETo access the instance and create an enumeration is thread-safe by default, and it also prevents problems caused by deserialization.
public enum Single {
INSTANCE;
public void whateverMethod(){};
}
personal blog:http://yhch.xyz
1. Definition Singleton Pattern: The singleton pattern ensures that a certain class has only one instance, and it instantiates itself and provides this instance to...
A single case pattern related concept 1 Definition:Make sure there is only one instance of a class, and you instantiate this instance to the entire system. 2 Features:(1) Only one instance (2) must cr...
You can use a single mode when you only need an instance or a global access point in the system. advantage: Save the resources of the system to create objects, improve the efficiency of the system, pr...
Singleton mode The first thing to understand is the design pattern. The design pattern is a set of reviews that have been used repeatedly, catalogued, and code design experience. The purpose of using ...
The concept of the singleton pattern is to ensure that a class has only one instance and provides a global access point to access it. That is to say, create multiple objects, each variable actually re...
717. 1-bit and 2-bit Characters We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). Now given a string ...
20. Sequence structure The most basic structure of Java is the sequential structure, unless otherwise specified, it is executed in order, sentence by sentence Sequence structure is the simplest algori...
Reproduced in: https: //blog.51cto.com/frankniefaquan/1934028...
First take a look at the definition of StringBuffer Defined as a final form, mainly for the sake of "efficiency" and "security". Its parent class AbstractStringBuilder is defined a...
linux a keyword query in the log The above xx into your log file name, keywords into your keyword like For example, you want to find ERROR level log your log file is the following command, xx or chang...