cool hit counter Two small examples to explain the "double locking" in the singleton pattern_Intefrankly

Two small examples to explain the "double locking" in the singleton pattern


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/26397949

When learning the singleton pattern, so many people don't quite understand Double lock . A very interesting example popped into my head after studying it.

Structure diagram of the singleton model.

Code.

Singleton class

    class Singleton
    {
        private static Singleton instance;
         private static readonly object syncRoot = new object();   // A static read-only process helper object is created at program runtime
         private Singleton() { } //modify the constructor with private to prevent outsiders from creating such instances with new
        public static Singleton GetInstance()
        {
             if (instance == null) // if instance object is null, then continue
            {
                 lock (syncRoot) //↓↓↓↓ is locked at the same moment only one thread of this part of the program can access ↓↓↓↓
                {
                     if (instance == null)//If instance object is null, then allocate instance for it
                    {
                        instance = new Singleton();
                    }
                 } //↑↑↑↑ has a lock on this part of the program at the same moment only one thread can access ↑↑↑↑
            } 
            return instance;
        }
    }

Client code

    static void Main(string[] args)
    {
        Singleton singleton1=Singleton.GetInstance();
        Singleton singleton2=Singleton.GetInstance();
        //……
    }

Description.

In Big Talk Design Patterns, Dish asks, "I've already determined outside if the instance instance exists, why do I have to make a determination inside the lock if the instance instance exists? "

Here's how Big Bird answered: when instance is null and two threads call the GetInstance method at the same time, they will both be able to pass the first heavy instance==null judgment. Then, because of the lock mechanism, only one of the two threads enters, and the other one waits in the queue outside, and one of them must enter and exit before the other can enter. And if there is no more second instance to determine whether the instance is null, then the first thread creates the instance, and the second thread can still continue to create new instances again. This defeats the purpose of the single instance.


Recommended>>
1、Adams2017 Software Installation Steps
2、China Early Childhood Education Industry Association Style ReportMember Overseas Chinese Big Data Co
3、UBER car kills man automated drones good helper for humans or coldblooded killer
4、EverSync the browser bookmarklet plugin the best choice to solve the synchronization of favorites
5、In the future Smarts Smartphone will completely replace smartphones

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号