栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

为什么锁{this}不好?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么锁{this}不好?

this
在锁语句中使用它是一种不好的形式,因为通常是您无法控制的其他人可能会锁定该对象。

为了正确计划并行操作,应格外小心,以考虑可能的死锁情况,并且锁入口点的数量未知会阻止这种情况。例如,任何引用对象的人都可以在对象设计者/创建者不知道它的情况下锁定它。这增加了多线程解决方案的复杂性,并可能影响其正确性。

通常,私有字段是一个更好的选择,因为编译器将强制对其进行访问限制,并且它将封装锁定机制。使用

this
通过向公众公开您的锁定实现的一部分来违反封装。同样不清楚的是,
this
除非已记录,否则是否将获得锁定。即使那样,依靠文档来防止问题也不是很理想。

最后,存在一个普遍的误解,

lock(this)
实际上是修改了作为参数传递的对象,并以某种方式使其变为只读或不可访问。这是 错误的
。作为参数传递的对象
lock
仅用作 。如果该键上已经有锁,则无法进行该锁;否则,将允许锁定。

这就是为什么在

lock
语句中使用字符串作为键很不好的原因,因为它们是不可变的,并且可以在应用程序的各个部分之间共享/访问。您应该改用私有变量,
Object
实例可以很好地完成任务。

运行以下C#代码作为示例。

public class Person{    public int Age { get; set;  }    public string Name { get; set; }    public void LockThis()    {        lock (this)        { System.Threading.Thread.Sleep(10000);        }    }}class Program{    static void Main(string[] args)    {        var nancy = new Person {Name = "Nancy Drew", Age = 15};        var a = new Thread(nancy.LockThis);        a.Start();        var b = new Thread(Timewarp);        b.Start(nancy);        Thread.Sleep(10);        var anotherNancy = new Person { Name = "Nancy Drew", Age = 50 };        var c = new Thread(NameChange);        c.Start(anotherNancy);        a.Join();        Console.ReadLine();    }    static void Timewarp(object subject)    {        var person = subject as Person;        if (person == null) throw new ArgumentNullException("subject");        // A lock does not make the object read-only.        lock (person.Name)        { while (person.Age <= 23) {     // There will be a lock on 'person' due to the LockThis method running in another thread     if (Monitor.TryEnter(person, 10) == false)     {         Console.WriteLine("'this' person is locked!");     }     else Monitor.Exit(person);     person.Age++;     if(person.Age == 18)     {         // Changing the 'person.Name' value doesn't change the lock...         person.Name = "Nancy Smith";     }     Console.WriteLine("{0} is {1} years old.", person.Name, person.Age); }        }    }    static void NameChange(object subject)    {        var person = subject as Person;        if (person == null) throw new ArgumentNullException("subject");        // You should avoid locking on strings, since they are immutable.        if (Monitor.TryEnter(person.Name, 30) == false)        { Console.WriteLine("Failed to obtain lock on 50 year old Nancy, because Timewarp(object) locked on string "Nancy Drew".");        }        else Monitor.Exit(person.Name);        if (Monitor.TryEnter("Nancy Drew", 30) == false)        { Console.WriteLine("Failed to obtain lock using 'Nancy Drew' literal, locked by 'person.Name' since both are the same object thanks to inlining!");        }        else Monitor.Exit("Nancy Drew");        if (Monitor.TryEnter(person.Name, 10000))        { string oldName = person.Name; person.Name = "Nancy Callahan"; Console.WriteLine("Name changed from '{0}' to '{1}'.", oldName, person.Name);        }        else Monitor.Exit(person.Name);    }}

控制台输出

'this' person is locked!Nancy Drew is 16 years old.'this' person is locked!Nancy Drew is 17 years old.Failed to obtain lock on 50 year old Nancy, because Timewarp(object) locked on string "Nancy Drew".'this' person is locked!Nancy Smith is 18 years old.'this' person is locked!Nancy Smith is 19 years old.'this' person is locked!Nancy Smith is 20 years old.Failed to obtain lock using 'Nancy Drew' literal, locked by 'person.Name' since both are the same object thanks to inlining!'this' person is locked!Nancy Smith is 21 years old.'this' person is locked!Nancy Smith is 22 years old.'this' person is locked!Nancy Smith is 23 years old.'this' person is locked!Nancy Smith is 24 years old.Name changed from 'Nancy Drew' to 'Nancy Callahan'.


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/376270.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号