Concept:
In Java, the singleton pattern is a common design pattern, there are several types of single-instance pattern, here are 2 kinds: Lazy Type single case, a hungry man type single case.
The singleton mode has the following characteristics:
1. A singleton class can have only one instance.
2. The Singleton class must create its own unique instance, requiring the constructor to be privatized to prevent the outside world from creating instances
3. The Singleton class must provide this instance to all other objects. Provides public static methods for a common external access to this instance
One, lazy type
Lazy Singleton: Instantiate yourself at the first call
Public singleton_lazy{
private static Singleton_lazy Single=null;
Private Singleton_lazy () {
}
Static Factory Method: Provides a common static method to return an instance to a common external call
public static Singleton_lazy getinstance () {
if (single==null) {
Single=new Singleton_lazy ();
}
return single;
}
}
Singleton_lazy by restricting the construction method to private to prevent the class from being instantiated externally, the unique instance of Singleton_lazy can only be accessed through the getinstance () method within the same virtual machine.
(In fact, through the Java reflection mechanism is able to instantiate the construction method for the private class, which will basically invalidate all Java Singleton implementation, this problem is not discussed here, when the reflection mechanism does not exist)
However, the above-mentioned lazy singleton implementation does not take into account the thread safety problem, it is thread insecure, the concurrency environment is likely to have multiple instances, to achieve thread safety, there are three ways, are the GetInstance method to transform, to ensure that the lazy single-case thread safety.
1, on the GetInstance method and synchronize
public static synchronized Singleton_lazy getinstance () {
if (single==null) {
Single=new Singleton_lazy ();
}
return single;
}
2. Double check Lock
public static Singleton_lazy getinstance () {
if (single==null) {
Synchronized (Singleton_lazy.class) {
if (single==null) {
Single=new Singleton_lazy ();
}
}
}
return single;
}
3. Static Inner class
public class Singleton_static {
private Static Class temp{
private static final singleton_static ss=new singleton_static ();
}
public static singleton_static getinstance () {
return TEMP.SS;
}
}
This is better than the above 1, 2, that is, the implementation of thread safety, but also avoid the performance impact of synchronization
Two, a hungry man type single case
A hungry man singleton, self-instantiated at class initialization
public class Singleton_eager {
private static final Singleton_eager se=new Singleton_eager;
Private Singleton_eager () {
}
public static Singleton_eager getinstance () {
return SE;
}
}
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 inherently thread-safe.
A hungry man and lazy-type difference
From the name of the A hungry man and the lazy,
A hungry man is the class once loaded, the singleton initialization is completed, to ensure that the getinstance, the singleton is already exist,
And lazy idle, only when the call getinstance, only to go back to initialize the singleton.
In addition differentiates the following two ways from the following two points:
1. Thread Safety:
A hungry man is inherently thread-safe and can be used directly for multithreading without problems.
The lazy type itself is non-thread-safe, in order to achieve thread safety there are several ways to write, respectively, the above 1, 2, 3, these three implementations in the resource load and performance of some differences.
2. Resource Loading and performance:
A hungry man a static object is instantiated at the same time as the class is created, and will occupy a certain amount of memory regardless of whether the singleton will be used later, but correspondingly, the speed will be faster on the first call because its resources have been initialized,
And the lazy type as the name implies, will delay loading, in the first use of the singleton when the object will be instantiated, the first call to do the initialization, if you want to do more work, performance will be somewhat delayed, and then the same as the A Hungry man type.
There are some differences between the three implementations of 1, 2 and 3,
1th, in the method call added synchronization, although thread security, but each time to synchronize, will affect performance, after all, 99% of the case is not required to synchronize,
2nd, two null checks were made in getinstance to ensure that only the first invocation of the singleton will be synchronized, which is also thread-safe, while avoiding the performance loss of each synchronization
3rd, using the ClassLoader mechanism to ensure that there is only one thread when initializing instance, so it is thread-safe, and there is no performance loss, so I tend to use this one.
Java design Pattern--Singleton mode