Use and comparison of several Singleton Modes

Source: Internet
Author: User

Use and comparison of several Singleton Modes

The use of a singleton is generally divided into three steps:

(1) The simplest way

 

 

Private static AudioManager mInstance; // Step 1: A private static class member variable private AudioManager () {}// Step 2: A private constructor public static AudioManager getInstance () {// Step 3: Obtain the instance externally using a public method. if (mInstance = null) {mInstance = new AudioManager ();} return mInstance ;}

 

This method works normally only when a single thread is running. In the case of multiple threads, if the two threads run the if statement to determine whether mInstance is null at the same time, and mInstance is not created, at this time, both threads go back to create an instance, which does not meet the requirements of a single instance.

 

 

(2) working in multiple threads with a low line rate

As can be seen from (1), in order to make it work normally under multiple threads, you need to add a synchronization lock, the modification is as follows:

 

Private static AudioManager mInstance; // Step 1: A private static class member variable private AudioManager () {}// Step 2: A private constructor public static AudioManager getInstance () {// Step 3: A public method for obtaining the instance externally synchronized (AudioManager. class) {if (mInstance = null) {mInstance = new AudioManager () ;}} return mInstance ;}
With this synchronization lock method, we can ensure that only one instance of this class is obtained in a multi-threaded environment, but every time we get an instance through the getInstance () method, all attempts to add a synchronization lock, and locking is a very time-consuming operation. When the instance exists, we should try to avoid locking again.

 


(3) Recommended Methods

 

 

/*** Use a single example to obtain the AudioManager object */private static AudioManager mInstance; // Step 1: private static class member variable private AudioManager () {} // Step 2: A private constructor public static AudioManager getInstance () {// Step 3: public method for obtaining instances externally if (mInstance = null) {synchronized (AudioManager. class) {if (mInstance = null) {mInstance = new AudioManager () ;}} return mInstance ;}

If you need to input parameters to the constructor when obtaining an instance, simply add the parameters in getInstance (), for example:

 

 

private String mDir;
Private static AudioManager mInstance; // Step 1: A private static class member variable private AudioManager (String dir) {// Step 2: A private constructor mDir = dir ;} public static AudioManager getInstance (String dir) {// Step 3: Obtain the instance externally using a public method if (mInstance = null) {synchronized (AudioManager. class) {if (mInstance = null) {mInstance = new AudioManager (dir) ;}} return mInstance ;}

 

The preceding getInstance method uses the if statement twice to determine that the lock operation is required only when mInstance is null and is not created. Because the mInstance is null at the first time, you only need to lock the instance when trying to create the instance for the first time, which greatly improves the efficiency compared with the second method.


 

 

 

 

 

 

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.