The intent of the singleton pattern is to ensure that a class has only one instance in the entire JVM and provides him with a global access point.
single case mode mechanism
How to prevent other developers from creating new instances of the class.
You can create a unique constructor and set it to private.
single case and thread
Multithreading delay Initializes a single case pattern to try to avoid multiple threads initializing the single Instance object at the same time.
In order to avoid this competition, it is necessary to use a locking mechanism to coordinate the execution of different threads on the same method, assuming that a thread discovers that the single Instance object is null, and then the second thread runs and finds that the single Instance object is null and then two threads initialize the single Instance object.
The public class singleton{
//holds a private static instance to prevent reference, assigning null here to implement deferred loading
private static Singleton instance=null;
Constructs a private constructor method to prevent instantiation of private
Singleton () {}
//Static engineering methods, creating an instance public
static Singleton getinstance () {
if ( instance = = null) {
instance = new Singleton ();
}
return instance
}
}
This single case pattern is not thread-safe and is placed in a multithreaded environment, and typically only needs to be locked for the first time the object is created:
public static Singleton getinstance () {
if (instance = null) {
synchronized (instance) {
if (instance = = NULL) {
instance = new Singleton ();
}}} return instance;
}
This situation may be problematic because the creation of objects and assignments in the Java directive is separate, meaning that instance = new Singleton () is two-step operation.
For example, there are a, b two threads:
1, A, b two threads at the same time into the if judgment, random to A line enters upgradeable to do if judgment
2, at this time instance is null, the implementation of instance = new SingleTon () operation, due to the JVM internal optimization mechanism, the JVM first allocated to the SingleTon instance of some blank memory, and assign it to the instance member (the JVM does not instantiate Singleton at this time), and then a leaves synchronized
3, then the B thread into the synchronized module, because Instance≠null, he will immediately leave the program block, and the program results returned to the method of the program.
4, at this point B thread is going to use singleton instance, but found the error, he actually did not initialize
Thus further optimization:
public static Class Singletonfactory () {
private static SingleTon instance = new SingleTon ();
public static SingleTon getinstance () {return
singletonfactory.instance;
}
}
At this point, Singleton will only be instantiated once
Only one instance of the JVM exists, and for some classes that need to be created frequently, it saves a lot of overhead in a large system, eliminates the new operation, reduces the utilization of system memory and the pressure of the GC, controls the core trading engine, prevents system confusion, and ensures that the core server controls the entire process independently