Title: Designing a class, we can only generate an instance of the class.
Solution One: Lazy type single case
1. For single-threaded environments
Lazy-Type singleton class. Instantiate yourself public class Singleton { //private default constructor private Singleton () {} ///Note that there is no final at the first call private static Singleton single=null; Static Factory method Public static Singleton getinstance () { if (single = null) {Single = new Singleton (); } return single; } }
the above-mentioned lazy single-case implementation does not take into account the thread safety problem, it is thread insecure, the concurrency environment is likely to appear multiple singleton instances。
Imagine that two threads are running at the same time to the IF statement that determines if single is null, and that if none is created, then two threads will create an instance, and this will not satisfy the requirements of the singleton mode.
2. Multi-Threading but not high efficiency
Add a sync Lock
public class Singleton { //private default constructor Singleton () {} //Note, there is no final private static Singleton Single=null; Static Factory method Public static Singleton getinstance () { synchronized (this) { if (single = null) {Single = new S Ingleton (); } } return single; } }
3. Two times before and after the synchronization lock to determine whether the instance exists
Lock time. You can implement a lock operation only if single is null and not created, and no lock is required when single is created.
public class Singleton { //private default constructor Singleton () {} //Note, there is no final private static Singleton Single=null; Static Factory method Public static Singleton getinstance () { if (single = null) { synchronized (this) { if (single = NULL) {Single = new Singleton (); }}} return single; } }
Solution Two: A hungry man type single case
A hungry man type Singleton class. At class initialization, the public class Singleton2 { //private default constructor child private Singleton2 () has been instantiated by itself. Private static final Singleton2 single = new Singleton1 (); Static Factory method Public static Singleton1 getinstance () { return single; }
a hungry man in the creation of a class at the same time has created a static object for the system to use, no longer change, so it is thread-safe.
Solution Three: A single case of registration
Similar to the method in spring, the class name is registered, the next time from the inside to get directly. public class Singleton3 {private static map<string,singleton3> Map = new Hashmap<string,singleton3> ( ); static{Singleton3 single = new Singleton3 (); Map.put (Single.getclass (). GetName (), single); }//protected by default constructor protected Singleton3 () {}//static Factory method, return the unique instance of this class public static Singleton3 Geti Nstance (String name) {if (name = = null) {name = Singleton3.class.getName (); System.out.println ("name = = NULL" + "--->name=" +name); } if (Map.get (name) = = null) {try {map.put (name, Singleton3) class.fo Rname (name). newinstance ()); } catch (Instantiationexception e) {e.printstacktrace (); } catch (Illegalaccessexception e) {e.printstacktrace (); } catch (ClassnotfoundeXception e) {e.printstacktrace (); }} return Map.get (name); }//A schematic commercial method public String about () {return "Hello, I am Regsingleton."; } public static void Main (string[] args) {Singleton3 single3 = singleton3.getinstance (null); System.out.println (Single3.about ()); } }
A registered Singleton actually maintains an instance of a set of singleton classes that are stored in a map (the register), returned directly from the map for an instance that has already been registered, and then registered and returned, if not registered.
A hungry man and lazy-type difference
These two look very similar at first glance, in fact there is a difference, the main two points
1. Thread Safety:
A hungry man-type is thread-safe, can be used directly for multithreading without problems, lazy-style is not, it is thread insecure, if used for multithreading may be instantiated multiple times, lost the role of Singleton.
If you want to use the lazy type for multi-threading, there are two ways to ensure security, one is to synchronize the GetInstance method, and the other is to use the Singleton method before and after the double lock.
2. Resource loading:
A hungry man a static object is instantiated at the same time as the class is created, regardless of whether the singleton will be used later, it takes up a certain amount of memory, and the corresponding speed will be faster at the time of the call.
And the lazy type as the name implies, will delay the loading, in the first use of the singleton when the object will be instantiated, the first time off to initialize, if you want to do more work, performance will be some delay, after the same as the A hungry man-style.
Reference:
http://blog.csdn.net/jason0539/article/details/23297037 (classic must SEE)
The offer-of the sword means to realize the singleton mode