为什么是不好的做法,锁定我们要改变的对象吗? [英] Why is it a bad practice to lock the object we are going to change?

查看:92
本文介绍了为什么是不好的做法,锁定我们要改变的对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么是不好的做法,用锁,如下面code,我假设这是根据<一个答案一种不好的做法href=\"http://stackoverflow.com/questions/7078535/c-sharp-lock-on-reference-passed-to-method-bad-practice\">this SO问题在这里

Why is it a bad practice to use lock as in the following code, I'm assuming this is a bad practice based on the answers in this SO question here

private void DoSomethingUseLess()
{
    List<IProduct> otherProductList = new List<IProduct>();
    Parallel.ForEach(myOriginalProductList, product =>
        {
           //Some code here removed for brevity
           //Some more code here :)
            lock (otherProductList)
            {
                otherProductList.Add((IProduct)product.Clone());
            }
        });
}

提到,这是不好的做法,但他们没有说为什么的答案

The answers over there mentions that it is bad practice , but they don't say why

注意:请忽略code的效用,这只是举例的目的,我知道这是不是在所有有用

Note: Please ignore the usefulness of the code, this is just for example purpose and i know it is not at all useful

推荐答案

从C#语言参考

在一般情况下,避免锁定在一个公开的类型,或超过code的控制情况。共同构建锁(本)锁(typeof运算(的MyType))锁(myLock)违反此准则:

In general, avoid locking on a public type, or instances beyond your code's control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:

锁(本)是,如果实例可以公开访问的问题。

lock (this) is a problem if the instance can be accessed publicly.

锁(typeof运算(的MyType))是,如果的MyType 的一个问题是公开访问。

lock (typeof (MyType)) is a problem if MyType is publicly accessible.

锁(myLock)是一个问题,因为任何其他code的过程中
  使用相同字符串,将共享相同的锁。

lock("myLock") is a problem because any other code in the process using the same string, will share the same lock.

最佳做法是定义一个私有对象锁定,或者私人
  静态对象变量,以保护共同所有实例的数据。

Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.

在你的情况,我就看了上面的指导,这表明锁定你将要修改的集合是不好的做法。例如,如果你写了这个code:

In your case, I would read the above guidance as suggesting that locking on the collection you will be modifying is bad practise. For example, if you wrote this code:

lock (otherProductList) 
{
    otherProductList = new List<IProduct>(); 
}

...那么你的锁将毫无价值。由于这些原因,建议使用专用对象变量锁定。

请注意,这并不意味着你的应用程序的破解的,如果你使用code您发布。 最佳做法通常被定义为提供易于重复的模式,在技术上更为抗跌。也就是说,如果你遵循最佳实践,并有一个专门的锁定对象,你是极不可能的曾经的写破锁定基于code;如果你不遵循最佳pracise那么,也许有时间在一百,你会得到通过易于避免的问题咬伤。

Note that this doesn't mean your application will break if you use the code you posted. "Best practises" are usually defined to provide easily-repeated patterns that are more technically resilient. That is, if you follow best practise and have a dedicated "lock object," you are highly unlikely to ever write broken lock-based code; if you don't follow best pracise then, maybe one time in a hundred, you'll get bitten by an easily-avoided problem.

此外(和更普遍),code。使用最佳实践编写通常更容易修改,因为你可以少警惕意料之外的副作用。

Additionally (and more generally), code written using best practises is typically more easily modified, because you can be less wary of unexpected side-effects.

这篇关于为什么是不好的做法,锁定我们要改变的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆