Quickly understand five of the singleton patterns in Java

Source: Internet
Author: User

Solution One: only for single-threaded environment (BAD)

Package test;/** * @author Xiaoping * */public class Singleton {    private static Singleton instance=null;    Private Singleton () {            } public    static Singleton getinstance () {        if (instance==null) {            instance=new Singleton ();        }        return instance;}    }

Note: In singleton static property instance, only one instance is created when instance is null, and the constructor is private, ensuring that only one is created at a time, avoiding duplicate creation.
Disadvantage: Only in the case of single-threaded normal operation, in the case of multi-threaded, there will be problems. For example, when two threads run to the IF statement that determines whether instance is empty, and instance is indeed not created, then two threads will create an instance.

Solution two: Multi-threaded situation can be used. (Lazy style, bad)

public class Singleton {    private static Singleton instance=null;    Private Singleton () {            } public    static synchronized Singleton getinstance () {        if (instance==null) {            Instance=new Singleton ();        }        return instance;}    }

Note: A synchronous lock is added on the basis of solution one, which makes it possible to use in multi-threaded situations. For example, when two threads want to create an instance at the same time, because only one thread can get a synchronous lock at a time, when the first thread is locked, the second thread waits only. The first thread discovery instance was not created, created. The first thread releases the sync lock, and the second thread can add a synchronous lock, executing the following code. Because the first thread has already created the instance, the second thread does not need to create an instance. Ensure that there is only one instance in the multi-threaded environment.
Cons: Every time you get a singleton instance through the GetInstance method, there is a process to try to get a sync lock. And it is well known that locking is time-consuming. Can be avoided by avoiding it.

Solution Three: Add synchronous lock, before and after two times to determine whether the instance exists (feasible)

public class Singleton {    private static Singleton instance=null;    Private Singleton () {            } public    static Singleton getinstance () {        if (instance==null) {            synchronized (Singleton.class) {                if (instance==null) {                    instance=new Singleton ();}}        }        return instance;}    }

Note: When instance is null, you need to get the synchronization lock and create the instance once. When an instance is created, there is no need to attempt to lock it.
Cons: Use double if to judge, complex, error prone.

Solution four: A Hungry man type (recommended use)

public class Singleton {    private static Singleton instance=new Singleton ();    Private Singleton () {            } public    static Singleton getinstance () {        return instance;    }}

Note: initial instance of static is created once. If we write a static method in the Singleton class that does not need to create an instance, it will still create an instance early. and reduce the memory usage.

Cons: No lazy loading effect, which reduces memory utilization.

Solution Five: Inside the static inside. (recommended)

public class Singleton {    private Singleton () {            }    private static class singletonholder{        private final Static Singleton instance=new Singleton ();    }    public static Singleton getinstance () {        return singletonholder.instance;    }}

Note: Defines a private inner class that, when used for the first time, creates an instance of the nested class. Whereas a class of type Singletonholder, called only in Singleton.getinstance (), cannot be used by others because of a private property, Singleholder is not called singleton.getinstance () The instance is not created.
Pros: The lazy loading effect is achieved by creating an instance on demand.

Quickly understand five of the singleton patterns in Java

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.