The Singleton mode is simple, that is, to ensure that an object is globally unique.
The Singleton mode is simple, that is, to ensure that an object is globally unique.
Right? True or not depends on the Standard
Singleton Pattern: make sure that a class has only one instance, and instantiate and provide this instance to the entire system,
This class is called a singleton class, which provides global access methods. The Singleton mode is an object creation mode.
It seems that I have not defined all of them.
The first guarantee class only has one instance
The second class is self-instantiated (meaning that other classes cannot be new to itself)
Third, provide the global access method.
OK. Now I want to write the first Singleton.
Singleton
Public class Singleton {private static Singleton sl; private Singleton () {System. out. println (initialization);} public static Singleton getInstance () {if (sl = null) sl = new Singleton (); return sl ;} public static void main (String [] args) {Singleton sl = Singleton. getInstance (); Singleton sl2 = Singleton. getInstance ();}}
The running result shows an initialization
However, there is a problem. If multiple threads run this code at the same time, when the first thread runs to sl = new Singleton () (this sentence has not been executed yet ), the second thread checks whether sl is null. At this time, the second thread also comes in... then there are multiple objects.
So there are
Singleton
/***** Hunger mode */public class Singleton2 {private final static Singleton2 sl2 = new Singleton2 (); private Singleton2 () {System. out. println (initialization);} public static Singleton2 getInstance () {return sl2;} public static void main (String [] args) {Singleton2 sl2 = Singleton2.getInstance (); singleton2 sl3 = Singleton2.getInstance ();}}
In this Singleton mode, the class variable is new when the class is loaded. This method is called the hunger model.
First, the hunger problem solved the above thread problem. At the beginning of loading, I had a class variable and only one variable. Naturally, there will be no thread problems.
However, there are personality issues here. No matter whether I use this Singleton class, it will always be loaded into the memory, so performance is a problem.
As mentioned above, this mode is called the hungry Chinese mode. Naturally, there is a non-hungry Chinese Mode (also called the lazy Chinese mode ). However, I don't want to talk about lazy models here. They are too complicated and not very useful.
Is there a singleton that can solve the first thread problem while maintaining performance (in fact, the second mode is good, that class, how much performance can be dragged? Even if there are 10, 100 are not too many? What's more, it's not as complicated as the lazy model I mentioned?
The answer is yes. Yes!
Internal Class Mode
Public class Singleton3 {private static class hold {private final static Singleton3 sl3 = new Singleton3 ();} private Singleton3 () {System. out. println (initialization);} public static Singleton3 getInstance () {return hold. sl3;} public static void main (String [] args) {Singleton3 sl3 = Singleton3.getInstance (); Singleton3 sl4 = Singleton3.getInstance (); System. out. println (sl3 = sl4 );}}
We will not talk about the analysis of internal classes and static and final.
The internal class method solves the problems mentioned above.
But it also raises a problem that is not a problem.
Internal classes are supported by java, but not necessarily in other languages.