Java Singleton mode and java mode
I. What is Singleton mode?
Singleton: ensure that a class has only one instance, and provide a global access point to access it.
The Singleton mode is one of the common software design patterns. It aims to ensure that only one unique instance of the class exists in the entire application.
For example, when the system starts, we need to load some public configuration information, which is visible and unique throughout the entire lifecycle of the application. In this case, we need to design the singleton mode. Such as spring container, session factory, cache, and database connection pool.
Ii. How to Ensure the uniqueness of an instance
1) prevent external Initialization
2) instantiate the class itself
3) ensure that the instance is instantiated once.
4) provides methods for obtaining instances.
5) thread security
Iii. Comparison of several single-profit models
(1) Hungry Chinese
"Because you are hungry, it is imperative to eat immediately." The static private variables of the class are instantiated at the same time.
Public class Singleton {
Private static final Singleton singleton = new Singleton ();
Private Singleton (){
}
Public static Singleton getInstance (){
Return singleton;
}
}
① Declare static private class variables and instantiate them immediately to ensure one instantiation
② Private structure to prevent external instantiation (reflection can be instantiated without considering this situation)
③ Provide the public getInstance () method for obtaining the singleton instance externally
Benefits: thread security; fast instance acquisition speed disadvantages: class loading means instance initialization, and memory waste
(2) lazy
"This person is relatively lazy and will be instantiated only when you are using it", delaying loading.
Public class Singleton {
Private static Singleton singleton = null;
Private Singleton (){
}
Public static Singleton getInstance (){
If (singleton = null ){
Singleton = new Singleton ();
}
Return singleton;
}
}
Advantage: Initialize the instance to save system resources.
Disadvantages: ① if initialization works much during instance acquisition, the loading speed slows down, affecting system performance
② Non-empty check is required for each instance acquisition, resulting in high system overhead.
③ Non-thread security: Multiple instances may be generated when multiple threads access getInstance () at the same time.
Next, we will perform thread security transformation:
1) Synchronization lock
Public synchronized static Singleton getInstance (){
If (singleton = null ){
Singleton = new Singleton ();
}
Return singleton;
}
Advantage: thread security. disadvantage: every time you get an instance, You need to lock it and consume resources. In fact, as long as the instance has been generated, you do not need to lock it later.
2) double check lock
Public static Singleton getInstance (){
If (singleton = null ){
Synchronized (Singleton. class ){
If (singleton = null ){
Singleton = new Singleton ();
}
}
}
Return singleton;
}
Advantage: thread security, dual check, ensure synchronization only before the instance is not initialized, high efficiency disadvantage: Still the instance is not empty judgment, consumes a certain amount of resources
3) static internal class
Public class Singleton {
Private Singleton (){
}
Private static class SingletonHolder {
Private static final Singleton singleton = new Singleton ();
}
Public static Singleton getInstance (){
Return SingletonHolder. singleton;
}
}
Advantage: it not only avoids performance loss caused by synchronous tapes, but also delays loading.
(3) Enumeration
Public enum Singleton {
INSTANCE;
Public void init (){
System. out. println ("Resource Initialization... ");
}
}
Natural thread security prevents reflection from generating instances.
Iv. Advantages and Disadvantages of Singleton Mode
Advantage: This class only has one instance, saving system resources. For objects that require frequent creation and destruction, the singleton mode can improve system performance.
Disadvantage: External instantiation (new) is not allowed. The caller is confused about which method to call to obtain the instance, especially when the source code is not visible.
Follow ginger to talk about technology, no.: helojava, or scan the following QR code.
One post per day, chicken soup for technical purposes.