Java design pattern: Singleton mode

Source: Internet
Author: User

Singleton Mode Overview

Singleton mode, the Singleton class can have only one instance in the entire application, and the Singleton class is responsible for creating this unique instance.

The single case model can be divided into a hungry man type single case and lazy type single case

A hungry man type single case

A A hungry man Singleton is a singleton class that is created when the instance is loaded, regardless of whether the instance will be used or not.

Code examples such as the following

class singleton{    privatestaticnew  Singleton ();     Private Singleton ()    {    }    publicstatic  Singleton getinstance ()    {        return  instance;}    }

Lazy Single-case

The lazy single case has been modified on the basis of the A Hungry man single case, and the Singleton class will be instantiated only if you want to get an instance.

Code examples are as follows

class singleton{    privatestaticnull;     Private Singleton ()    {    }    publicstatic  Singleton getinstance ()    {        if  null)            new  Singleton ();         return instance;}    }

A singleton mode under multi-threading

  Both of these singleton modes are simple to implement, and there is no problem with single-threaded, but it is possible to have problems in the case of multithreading. For example, the following scenario: There is a singleton class s, thread A calls S of getinstance () to create an instance, to determine the end of instance is null but at this time because the CPU times slicing switch thread B starts execution, Similarly creating an instance of the S class calls the GetInstance () method, at which point the instance is still null when thread B is created to switch to thread a execution, and thread A creates the instance instance directly because it has been instrumented. In this case, the S class creates two instances

Make changes in the lazy singleton form and put the creation action into the synchronized code block

class singleton{    privatestaticnull;     Private Singleton ()    {    }    publicsynchronizedstatic  Singleton getinstance ()    {         if NULL )            new  Singleton ();         return instance;}    }

Because of the efficiency of the synchronization method, the above method is not the best implementation.

Double check plus lock

  

1 classSingleton2 {3     Private StaticSingleton instance =NULL;4 5     PrivateSingleton ()6     {7     }8 9      Public StaticSingleton getinstance ()Ten     { One  A         if(Instance = =NULL) -         { -             synchronized(Singleton.class) the             { -                 if(Instance = =NULL) -Instance =NewSingleton (); -             } +         } -         returninstance; +     } A}

This makes use of the synchronous code block, when only instance is null, which is required to create instance, to do synchronous processing, if the instance already exists only need to return directly. This ensures the single-instance implementation in multi-threaded situations, while improving efficiency compared to the direct use of synchronous methods.

Java design pattern: Singleton mode

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.