Ensure that the class inside the system can have at most one instance object. Simple singleton Mode implementation:A. First define a private variable instance to instantiate the class object;B. Privatization of the construction method;c. Implement global access point public static Singleton GetInstance () method, and since the method is private, the variable instance is also defined as private. d. If the instance requires a more complex instantiation process, place the instantiation process in static{}:
Lazy Type:
Public class singleton{privatestaticnew Singleton (); // Defining instance Variables Private Singleton () {}; // privatization Construction Method Public Static Singleton getinstance () { return instance; }}
This implementation is thread-safe and does not instantiate multiple objects when accessed by multiple threads, because the static property is initialized only once, and the disadvantage is that the instance is initialized regardless of whether it is used, and the cost of doing so becomes larger.
A Hungry man type:
public class singleton{ private static Singleton instance = null; Private Singleton () {}; public static Singleton getinstance () { if (instance = = null) { instance = new Singleton (); } return instance;} }
This implementation guarantees that the object is initialized (deferred creation) only when the class is called, but the disadvantage is that the thread is unsafe and it is very likely to instantiate multiple objects when multiple threads are concurrently accessed.
Workaround: Add synchronization to the entire method
(public static synchronized Singleton getinstance ())
, but this solves the thread's security problem, but it greatly reduces the performance. There are some unnecessary synchronizations, such as the return operation, in fact, the real need to synchronize is to create the time, improve:
s private volatile static Singleton instance = NULL;
The instance object is modified with volatile, and volatile has synchronized visibility, so that the thread can automatically discover the latest value of the volatile variable so that one thread is instantiated successfully and other threads can immediately discover it.
public static Singleton getinstance () {if (instance = = null) { //Create a synchronization if not created synchronized (singleton.class) { if (instance = = null) { //re-judge, in case two threads at the same time after the first judgment, after successively entered the synchronization or can create their own objects, it is necessary to judge instance = new Singleton ();} } return instance; }
This is actually called (double-cheched-locking mode)
One drawback is that when a thread has not fully initialized the object, and that variable is already displayed as initialized, other threads may be able to use this instance that is not fully initialized, causing the system to crash. But this can run safely above java5.
Another perfectly implemented implementation is a thread-safe and deferred-loading pattern (initialization on demand holder) using the static inner class example:
public class singleton{ Private Singleton () {}; public static class singleton1{ Private static final Singleton instance = new Singleton ();} public static Singleton getinstance () { Return singleton1.instance;}}
This is actually called (double-cheched-locking mode)
One drawback is that when a thread has not fully initialized the object, and that variable is already displayed as initialized, other threads may be able to use this instance that is not fully initialized, causing the system to crash. But this can run safely above java5.
Another perfectly implemented implementation is a thread-safe and deferred-loading pattern (initialization on demand holder) using the static inner class example:
public class singleton{ Private Singleton () {}; public static class singleton1{ Private static final Singleton instance = new Singleton ();} public static Singleton getinstance () { Return singleton1.instance;}}
This ensures that the instance instance is initialized the first time the getinstance () method is called, and that the instance is defined as static and is initialized only once.
Another problem is the serialization of a singleton class: If the Singleton class implements the Serializable interface, it is important to note that, by default, a new object is always created at each deserialization, noting that multiple objects appear on the system. Workaround: According to serialization, The Readresolve () method is called before each deserialization is complete, in which case the original object is substituted for the newly created object. Define a method in the class where the serialization is implemented:
Public Singleton Readresolve () {
Sreturn instance; Instance is the only instance object
}
Single Case design mode