# # 1. Overview
> Singleton mode is to ensure that there is only one instance in a class.
----------
# # 2. A hungry man type single case
"' Java
public class Singletoninstance {
private static Singletoninstance minstance = new Singletoninstance ();
Default Private Constructor method
Private Singletoninstance () {}
Static Factory method
public static Singletoninstance getinstance () {
return minstance;
}
}
```
In the A Hungry man-type singleton, the static variable is initialized in the private constructor method, when the only instance is created, the A Hungry man type is mainly used to the idea of the * * Space Change time * *, when the class is still loading, the object instance has been created.
----------
# # # 3. Lazy type singleton
' Java
public class Singletoninstance {
private static singletoninstance minstance = null;
Private default constructor method
Private Singletoninstance () {}
//Static factory method
public static synchronized Singletoninstance getinstance () {
if (minstance = = null) {
Minstance = new Singletoninstance ();
}
return minstance;
}
}
"
for lazy single-case processing, using the ' synchronized ' parameter modification factory method, used in multi-threaded environment to solve the synchronization problem, lazy-type is mainly used in the * * Time for space * * idea, When the instance is taken, it is only necessary to create the object and save the memory space, but the disadvantage is that it is thread-safe, which reduces the speed of access.
----------
# # 4. Double Lock single case
"' Java
public class Singletoninstance {
Private volatile static singletoninstance minstance = null;
Private Singletoninstance () {}
public static Singletoninstance getinstance () {
Check that the instance exists before entering the following synchronization block if it does not exist
if (minstance = = null) {
Synchronization blocks, thread-safe creation instances
Synchronized (Singletoninstance. Class) {
Check again if the instance exists, and if it does not exist, actually create the instance
if (minstance = = null) {
Minstance = new Singletoninstance ();
}
}
}
return minstance;
}
}
```
Double locking mechanism refers to the "getinstance" method in each entry is not the first step, but into the method, first check whether the existence of the instance, if not exist before the following synchronization block, which belongs to the first check, after entering the synchronization block and then check whether the instance exists, if not exist, Creates a new instance in the case of synchronization, which is a second check. This allows you to synchronize only once, and reduces the time it takes to make a judgment waste in multiple synchronizations.
This implementation uses the **volatile** keyword, meaning that the value of the variable being modified by the **volatile** is not cached by the local thread, and all read and write to the variable is directly operating shared memory, thus ensuring that the thread handles the variable correctly.
> **volatile** keyword in the version prior to **jdk5** failed to lock, note. And the **volatile** keyword masks some of the necessary code optimizations in the virtual machine, so although you can implement a double-check locking mechanism for a single case, it is not recommended to use it extensively.
What is the solution that can both delay loading and thread safety?
-----
# # 5. Lazy initialization Holder class mode
"' Java
public class Singletoninstance {
Private Singletoninstance () {}
/**
* An instance of the class-level inner class has no binding relationship to an instance of an external class, and is only loaded when called to
*/
private Static Class singletonholder{
/**
* Static initialization, guaranteed by the JVM for thread safety
*/
private static Singletoninstance minstance = new Singletoninstance ();
}
public static Singletoninstance getinstance () {
return singletonholder.minstance;
}
}
```
If you simply want to implement a simple thread-safe singleton, you can use the previous * * * * * a hungry man method. But the disadvantage is that the object is initialized when the class is loaded, resulting in wasted space.
The problem can be resolved by resolving the problem of automatically initializing objects when the class loads. Therefore, the class-level inner class can be implemented in such a way that the object instance is created only when needed, and the purpose of lazy loading and thread safety is achieved.
From the implementation process, but when the ' getinstance ' method is called, it reads ' Singletonholder.minstance ', which initializes the static members when the class is loaded, and is initialized only once because of the nature of the static domain. And the JVM guarantees thread safety.
>-what is a class-level inner class?
The inner class of the member that is modified by ' static ' is the class-level inner class, and if not modified by ' static ' is called an object inner class, and the class-level inner class does not have a dependency relationship with the Outer class object, it is called only when it is first used.
>-Multithreading default sync lock knowledge?
In multithreaded development, we mainly use ' synchronized ' to synchronize the mutex, but in some cases the JVM has synchronized control for us, mainly:
1. Static initialization method when initializing data;
2. When accessing the ' final ' field;
3. When creating the object before creating the thread;
4. When the thread can see the object to be processed;
----------
# # 6. Enumeration single-Case
"' Java
public enum singletoninstace{
Defines an enumeration element that represents an instance of the
Minstance;
Operation of a single case
public void Singletonoperation () {
}
}
```
The way to use enumerations is to make the code concise, and the JVM to guarantee the serialization mechanism, to prevent multiple instantiation, is the best way to implement a single case.
"Java design mode" Singleton mode