The single-instance pattern is written in a number of forms, giving the most basic form:
(a type of notation):
Package Singleton;public class Singletoninstance {private static singletoninstance msingletoninstance = null;// Deliberately set the constructor to private to prevent external user new singletoninstance (). Private Singletoninstance () {}public static singletoninstance getinstance () {if (msingletoninstance = = null) { Msingletoninstance = new Singletoninstance ();} return msingletoninstance;}}
A notation is the simplest, most basic, and clearest form of a singleton pattern, but unfortunately this is a thread-insecure code notation. It would be a disaster to assume that the class is being accessed simultaneously in concurrent n multiple threads, especially if this class is sensitive to thread-safety issues such as database access and so on.
However, the A-way of the singleton pattern has its wide range of scenarios: in scenarios that do not require thread safety, have no synchronization requirements, and have high efficiency priorities, a one-way notation for singleton patterns is recommended.
b Notation (thread-safe notation 1):
Package Singleton;public class Singletoninstance {private static singletoninstance msingletoninstance = null;// Deliberately set the constructor to private to prevent external user new singletoninstance (). Private Singletoninstance () {}public static synchronized singletoninstance getinstance () {if (msingletoninstance = = NULL {msingletoninstance = new singletoninstance ();} return msingletoninstance;}}
The B type of the singleton pattern is actually the improvement on the basis of a kind of writing, the main point is to increase the synchronization mechanism: synchronized. Synchronized, synchronization in a sense is actually blocking, the result of blocking is any moment, only one thread can access the code in the synchronization method body. This will degrade the performance of the code being synchronized, but it achieves thread-safe purposes.
The formulation of the B-type Singleton pattern is primarily intended to address thread safety. Variations are many, and the purpose of the variants is mainly focused on how to enhance the operability of thread safety. Examples are as follows, for example:
Enhanced variants of B notation (thread-safe notation 2):
Package Singleton;public class Singletoninstance {//Note! Volatile also does not fully guarantee thread safety, and later writes about volatile articles explaining this bit. Volatile is only enhanced. private static volatile singletoninstance msingletoninstance = null;//deliberately sets the constructor to private to prevent external user new Singletoninstance ( )。 Private Singletoninstance () {}public static singletoninstance getinstance () {if (msingletoninstance = = null) {//Synchron Ized (Singletoninstance.class) does not have to lock the entire method, only a piece of code can be locked. Same as synchronized (this), but because it is a static method, it is not possible to use this to use the basic use principle of xxxclass.class//synchronized: Try not to lock large chunks of code (method body or Class) as much as possible. Just lock in the necessary piece of core, need to synchronize the code snippet, the less the better, the smaller the better. You know, once you use synchronized, it means the cost of code performance synchronized (Singletoninstance.class) {if (msingletoninstance = = null) Msingletoninstance = new Singletoninstance ();}} return msingletoninstance;}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Design patterns: How to Style a singleton (basic notation and thread-safe notation)