Preface: The singleton pattern everyone should be very familiar with, I am here to summarize myself in this period of time learned a single case related knowledge.
The purpose of the singleton mode is to ensure that a class has only a single instance, that is, you cannot create a new instance of the class by using new.
the meaning of the singleton pattern : to ensure that a class has only a single instance, that is, you cannot create a new instance of the class with new. We can control the number of generation of class objects.
Single-case mode use occasions:
There are 3 ways to use a single case:
1. Class A inherits from Class B, at which point a needs to use a singleton to directly access the variables and methods declared in B
2, Class A will be inherited by Class B, when a also need to use a singleton, otherwise B will not be able to directly use a method
3, the Singleton can provide a constructor, to ensure that the use of the singleton can initialize some environment, such as reading from the external configuration file, or external to establish basic communication, etc.
Several implementations (all implemented in a thread-safe manner):
1. A Hungry man type:
Public class Private Singleton () {System.out.println ("Singleton is create"Private Static New publicstatic return instance;}}
This realization is also called the A Hungry man-type singleton model, the purpose is that whether you want to, I first come out in advance, this is certainly thread-safe, because the new action occurs when the first time to access the class.
Disadvantage: Unnecessary instantiation may occur. That is, I do not want to get an instance, but it does it by itself, which reduces efficiency.
Example:
public class Singleton {public static int status=1;private Singleton () {System.out.println ("Singleton is Create");} private static Singleton instance = new Singleton ();p ublic static Singleton getinstance () {return instance;}}
I just want to access a static field inside the singleton, and as a result he also performs a new object operation.
2. Synchronization method:
Public classLazysingleton {PrivateLazysingleton () {System.out.println ("Lazysingleton is create"); } Private StaticLazysingleton instance =NULL; Public Static synchronizedLazysingleton getinstance () {if(Instance = =NULL) Instance=NewLazysingleton ();returninstance;} }
Although this approach solves the requirement of thread safety and unnecessary action, but because it is synchronized in the whole getinstance method, the GetInstance method can be called only one thread at a time, which will affect the efficiency of the program running.
3. Synchronizing code blocks (Double Check)
Public Resource getresource () { ifnull) { synchronized(This { if (resource==null) {new resource (); } } } return resource; }
A good solution, but first you need to declare a single case as a volatile type to ensure thread safety, followed by the use of synchronous code block, so it will affect efficiency, but than
Before a few good many.
4. Internal Static class:
Public classStaticsingleton {PrivateStaticsingleton () {System.out.println ("Staticsingleton is create"); } Private Static classSingletonholder {Private StaticStaticsingleton instance =NewStaticsingleton ();} Public StaticStaticsingleton getinstance () {returnsingletonholder.instance;} }
This is not a good way to sync, but it solves some of the drawbacks of the previous cases.
"Singleton mode" under high concurrency in Java