The singleton mode is a more common design pattern in 23 design patterns, and because of its reduced code volume, it is often used to test the interviewer's ability in Interviews.
The primary singleton pattern is simple
Implement two requirements
1 Construction Method Privatization
2 methods of providing static, public access to objects externally
So: The primary singleton pattern is as follows
public class Singelton {
Private Singelton () {}
private static Singelton sin=null;
public static Singelton Getsingelton () {
If (sin==null) {
Sin=new Singelton ();
}
Return sin;
}
}
----------------------------------------
But is that enough?
As we learn, we know that most programs run in multi-threaded Environments. This puts a higher quality requirement on our code and requires us to consider thread safety Issues.
The only piece of code above does not guarantee thread Security. So:
public class Singletonthread {
Private Singletonthread () {}
private static Singletonthread st=null;
Public synchronized static Singletonthread Getsingletonthread () {
If (st==null) {
St=new Singletonthread ();
}
Return st;
}
}
This code takes into account thread safety, but is the cost of locking the method too high? Efficiency is similar to a single thread, assuming that there are tens of thousands of lines of code in this method that lock on the method
It's not worth the Cost.
so, we have a better way.
1
Public final class Singletonone {//a hungry man type, cannot be implemented on demand
Private Singletonone () {};
private static Singletonone sin=new Singletonone ();
public static Singletonone Getsingleton () {
Return sin;
}
}
A static member is used only once during the class load phase to obtain a unique Object.
This method is not only thread safe, but also an introduction to METHODS.
2 Can we create an instance of the class from scratch? Do it On demand
As follows:
Public final class Singletontwo {
Private Singletontwo () {};
public static Singletontwo Setsin () {
Return singleton.sin;
}
Static class Singleton{//inner classes are no longer loaded when the external class is loaded, and therefore are on demand.
Private Singleton () {};
private static final Singletontwo sin=new Singletontwo ();
}
}
With the nature of the inner class not being loaded when the external class is loaded, the real implementation is on DEMAND.
---------------------------------------------
The above two methods are excellent, but also need to be used according to the actual situation, because the methods and properties in the class are static, has lost the meaning of the inheritance
So there's another way to recommend It:
3
public class Singletonv {
Private Singletonv () {}
Private volatile Singletonv singleton=null;
Public Singletonv Getsingleton () {
If (singleton==null) {
Synchronized (singletonv.class) {
If (singleton==null) {
Singleton=new Singletonv ();
}
}
}
Return singleton;
}
}
This class retains the meaning of inheritance, as well as locking, but with much less overhead. Uses the keyword Volatile.
The specific usage is as follows
In the current Java memory model, threads can store variables in local memory (such as the Machine's registers),
Instead of reading and writing directly in main memory. This can cause a thread to modify the value of a variable in main memory,
Another thread continues to use its copy of the variable value in the register, resulting in inconsistent Data.
To solve this problem, declare the variable to be volatile (unstable),
This indicates to the JVM that the variable is unstable and is read in main memory each time it is Used.
Generally speaking, the flags shared between tasks in a multitasking environment should be modified with a volatile modifier.
A volatile-modified member variable forces the value of the member variable to be reread from shared memory each time it is accessed by the Thread.
also, when a member variable changes, forcing the thread to write the change back to the shared memory. So at any moment,
Two different threads always see the same value for a member Variable.
Multiple implementation modes of Single-instance mode under multi-threading