Defined
Singleton mode ensures that a class has only one instance and provides a global access point.
Code implementation
There are several ways to implement a singleton pattern:
Eager instantiation of
Use "eager" to create an instance without delaying the instantiation procedure. Creating a singleton in a static initializer guarantees thread safety.
public class Singleton {//√private static Singleton instance = new Singleton ();p rivate Singleton () {}public static Singl Eton getinstance () {return instance;}}
Synchronous getinstance () method
Class Singleton2 {private static Singleton2 instance;private Singleton2 () {}public static synchronized Singleton2 getinst Ance () {if (instance = = null) {instance = new Singleton2 ();} return instance;}}
The practice of Synchronizing getinstance () will degrade performance (synchronizing a method may result in a 100 times-fold decrease in program execution), which is generally not used.
Double check plus lock
Class Singleton3 {//√, better than Singleton2 private volatile static Singleton3 instance;private Singleton3 () {}public sta Tic Singleton3 getinstance () {if (instance = = null) {synchronized (Singleton3.class) {if (instance = = null) {instance = new Singleton3 ();}}} return instance;}}
The volatile keyword ensures that when the instance variable is initialized to an Singleton3 instance, multiple threads handle the instance variable correctly.
Volatile and synchronized
Java has an idea called the "main" memory area, where the variable's current "exact value" is stored.
(1) A variable has to be synchronized in all threads after a volatile modification; any thread changes its value, and all other threads get the same value immediately;
(2) Look at the following section of code:
private int i3; synchronized int Geti3 () {return i3;}
First, synchronized obtains and releases the monitor--if two threads use the same object lock, the monitor can force the code block to be executed by only one thread at a time.
However, synchronized also synchronizes memory: in fact, synchronized synchronizes the entire thread's memory in the "primary" memory area. Therefore, the Execute Geti3 () method does the following steps:
1>. The thread requests to get an object lock that monitors the This object (assuming it is not locked, otherwise the thread waits until the lock is released)
2>. Thread memory data is eliminated and read from the "main" memory area (Java virtual function optimizes this step ...) [I don't know how to express it back, Khan])
3>. code block is executed
4>. Any change to a variable can now be safely written to the "main" memory area (although the Geti3 () method does not change the value of the variable)
5>. Thread Release object lock that monitors this object
Single-Case mode