RuPeng.HystrixCore icon indicating copy to clipboard operation
RuPeng.HystrixCore copied to clipboard

双if加锁的方式是否是更合理

Open Emrys5 opened this issue 6 years ago • 3 comments

  if (policy == null)
            {
                lock (this)//因为Invoke可能是并发调用,因此要确保policy赋值的线程安全
                {
                    if (policy == null)
                    {
                           ...
                     }
                  }
             }

Emrys5 avatar Jun 24 '18 13:06 Emrys5

嗯,闻到了坏代码的味道,来看了下,果然有哥们已经指出来了

xujiesh0510 avatar Jul 12 '18 12:07 xujiesh0510

谢谢。lock (policies)外面已经加锁了

yangzhongke avatar Jul 14 '18 04:07 yangzhongke

  if (policy == null)
            {
                lock (this)//因为Invoke可能是并发调用,因此要确保policy赋值的线程安全
                {
                    if (policy == null)
                    {
                           ...
                     }
                  }
             }

已经lock(policies)了,就用不着加双if了。

SuperSnowYao avatar Nov 11 '19 06:11 SuperSnowYao