Singleton Pattern)

Source: Internet
Author: User

Singleton mode ensures that a class has only one instance and provides a global access point.
Some objects only need one, such as the thread pool, cache, registry, and so on. If these classes have multiple instances, many problems may occur.
The Singleton mode ensures that only one of these global resources is used.
Implementation of a classic Singleton mode:
[Java]
Public class Singleton {
Private static Singleton uniqueInstance;
Private Singleton (){}
Public static Singleton getInstance (){
If (uniqueInstance = null ){
UniqueInstance = new Singleton ();
}
Return uniqueInstance;
}
}

Because the Singleton class does not have a public constructor, we cannot directly create instances of this class. Instead, we can only call the static getInstance () method to obtain a reference to the Singleton object. When getInstance () is called, if the class does not have any instances, an instance is created and a reference is returned. If an instance already exists, a reference of the instance is directly returned.
This ensures that the class of a single instance can only have one instance at most.

Hidden risks under multiple threads
In the case of multiple threads, what happens if two threads call the getInstance () method almost simultaneously? Two instances of this class may be created.
We can change the getInstance () method to the synchronization method to solve this problem:
[Java]
Public class Singleton {
Private static Singleton uniqueInstance;
Private Singleton (){}
Public static synchronized Singleton getInstance (){
If (uniqueInstance = null ){
UniqueInstance = new Singleton ();
}
Return uniqueInstance;
}
}

Performance problems
However, in fact, we only need to synchronize when the uniqueInstance is null. When this class already has instances, there is no multithreading risk.
Therefore, changing the getInstance () method to a synchronous method may greatly degrade the performance.
If you change the getInstance () method to the synchronous method, this Singleton can be created during static initialization.
[Java]
Public class Singleton {
Private static Singleton uniqueInstance = new Singleton ();
Private Singleton (){}
Public static Singleton getInstance (){
Return uniqueInstance;
}
}
This naturally ensures the Singleton.
The problem is that in the previous example, a singleton is created when an instance is required. In this example, a singleton is created during class initialization. If this object is very resource-consuming and it is never used in the program, it is a waste of resources.

"Double check lock"
[Java]
Public class Singleton {
Private volatile static Singleton uniqueInstance; // volatile modifies the variables accessed and modified by different threads
Private Singleton (){}
Public static Singleton getInstance (){
If (uniqueInstance = null ){
Synchronized (Singleton. class) {// lock the entire class
If (uniqueInstance = null ){
UniqueInstance = new Singleton ();
}
}
}
Return uniqueInstance;
}
}
This ensures correctness and efficiency while saving resources. The implementation method is not concise enough.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.