Several implementations of Java singleton classes

Source: Internet
Author: User

One, the simplest way
 Public class singleton{    private  Singleton () {};     Private Static New Singleton ();      Public Static Singleton getinstance () {        return  instance;    }}

First, the constructor is declared private, preventing instances of the class from being created externally. Declares a static member variable instance and allocates an instance, and when the Singleton class is loaded, instance is created and can be obtained through the static method GetInstance method.
The advantage is that it is simple to implement and has no thread safety issues. The disadvantage is that the instance instance is created when Singleton is referenced, even if the instance is not used.

Second, lazy loading of the singleton class
 Public classsingleton{PrivateSingleton () {}; Private StaticSingleton instance =NULL;  Public Static synchronizedSingleton getinstance () {if(Instance = =NULL) {instance=NewSingleton (); }        returninstance; }}

Because the instance initial value is null, the singleton is not instantiated when it is loaded, and the instance is created only when the GetInstance method is called. To prevent multi-threaded concurrent calls to the GetInstance method, instance is created multiple times, so thread synchronization is performed using the Synchronized keyword.
The disadvantage of this implementation is that each call to the GetInstance method will be thread-synchronized, affecting the amount of concurrency.

Third, improve lazy loading of the singleton class
 Public classsingleton{PrivateSingleton () {}; Private Static volatileSingleton instance =NULL;  Public StaticSingleton getinstance () {if(Instance = =NULL){            synchronized(Singleton.class){                if(Instance = =NULL) {instance=NewSingleton (); }            }        }        returninstance; }}

Optimized by double retrieval, thread synchronization occurs only when the intance has no value, and the instance is returned directly thereafter. It should be noted that in the synchronized code block it is necessary to determine whether the instance is null again, preventing multiple threads from passing the first null judgment at the same time.
It is also important to note that instance requires a volatile modifier to prevent errors caused by command reordering.
In addition to ensuring that the thread cache synchronizes to the main memory and cleans up the values of other thread caches in a timely manner, there is also a role in preventing command reordering. Instance = new Singleton () This line of code is compiled and split into three instructions, which can be interpreted as follows:
1,singleton temp = malloc ();//Allocate memory
2,constructor (temp); Invokes the constructor to initialize the allocated memory
3,instance = temp; Initialize the completed memory address assignment to instance
The compiler, in order to optimize the instructions, may become the following code after reordering:
1,singleton temp = malloc ();//Allocate memory
2,instance = temp; Initialize the completed memory address assignment to instance
3,constructor (instance); Invokes the constructor to initialize the allocated memory
If the code executes the second step above, instance has been assigned a value that is not NULL, but is not initialized, which is if the second thread calls the GetInstance method to get instance directly and throws an error when instance is called. Volatile can prevent reordering.

Four, through the inner class to implement the Singleton
 Public classsingleton{Private Static classsingletonholder{Private StaticSingleton instance =NewSingleton (); }    PrivateSingleton () {}; Private Static volatileSingleton instance =NULL;  Public StaticSingleton getinstance () {returnsingletonholder.instance; }}

The instance of Singleton is held by the static inner class Singletonholder, and the Singletonholder is loaded only when the GetInstance method is called, and instance is instantiated. Both lazy loading and no thread security issues.

Several implementations of Java singleton classes

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.