Design ModeSingle-piece mode: ensure that a class has only one instance and provides a global access point.
Key PointsSingle-piece mode ensures that a class in a program has only one instance at most. To implement the single-piece mode in Java, a private constructor, a static method, and a static variable are required. Determine the performance and resource restrictions, and then carefully select the appropriate solution to implement a single piece to solve the problem of multithreading.
Disadvantages of global variablesIf you assign an object to a global variable, you must create the object at the beginning of the program. In case the object is very resource-consuming and the program does not use it during this execution, a waste is formed.
Single-piece Mode
Public class Singleton {// use a static variable to record the unique private static Singleton uniqueInstance instance of the Singleton class; // declare the constructor as private and there is no public constructor, avoid Multiple instances. To obtain an instance, call Singleton. getInstance (). private Singleton () {}// static method, which is a "class" method. Reference a static method. Only the class name is required. The object name public static Singleton getInstance () {if (uniqueInstance = null) {uniqueInstance = new Singleton ();} is not required ();} return uniqueInstance ;}}
How to Improve multithreading?(1) If the performance of getInstance () is not critical to the application, it is simple and convenient to synchronize getInstance (). Although the program execution efficiency may be reduced by 100 times.
Public class Singleton {private static Singleton uniqueInstance; private Singleton () {}// Add the synchronzed keyword to this method, forcing each thread to enter this method, wait for other threads to leave this method. Public static synchronzed Singleton getInstance () {if (uniqueInstance = null) {uniqueInstance = new Singleton () ;}return uniqueInstance ;}}
(2) Use the method of urgently creating instances without delay instantiation.
Public class Singleton {// create a single piece in the static initiator. This Code ensures thread security: private static Singleton uniqueInstance = new Singleton (); private Singleton () {} public static Singleton getInstance () {// an instance exists, directly use return uniqueInstance ;}}
(3) Use "double check and lock" to reduce the use of synchronization in getInstance. First, check whether the instance has been created. If not, synchronize the instance. It is not applicable to version 1.4 or earlier.
Public class Singleton {// The volatile keyword ensures that when the uniqueInstance variable is initialized to a Singleton instance, multiple threads correctly process the variable private volatile static Singleton uniqueInstance; private Singleton () {} public static Singleton getInstance () {// check the instance. if the instance does not exist, if (uniqueInstance = null) {synchronized (Singleton. class) {// enter the block and check again. If it is still null, the instance is created. If (uniqueInstance = null) {uniqueInstance = new Singleton () ;}} return uniqueInstance ;}}