当前位置: 代码迷 >> ASP.NET >> 请教lock(obj)和Application.Lock有什么区别
  详细解决方案

请教lock(obj)和Application.Lock有什么区别

热度:5212   发布时间:2013-02-25 00:00:00.0
请问lock(obj)和Application.Lock有什么区别?
C# code
                int a = 0;                lock (Application["delDatetime"])                {                    a += 1;                    Application["delDatetime"] = System.DateTime.Now;                }


C# code
                int a = 0;                Application.Lock();                a += 1;                Application["delDatetime"] = System.DateTime.Now;                Application.UnLock();

请问在以上两段代码遇到并发的时候,是不是都能锁住a += 1?

------解决方案--------------------------------------------------------
看了下源码 事实 Application.Lock()的底层也是用 lock来实现的
C# code
internal virtual void AcquireWrite(){    lock (this)    {        while (this._lock != 0)        {            try            {                Monitor.Wait(this);                continue;            }            catch (ThreadInterruptedException)            {                continue;            }        }        this._lock = -1;    }}
  相关解决方案