为什么锁对象必须是静态的? [英] Why does the lock object have to be static?

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

问题描述

这是非常普遍使用私有静态只读对象多线程锁定。
据我了解,私人拧紧封装降低了入口点锁定对象,并因此获得最重要的。

It is very common to use a private static readonly object for locking in multi threading. I understand that private reduces the entry points to the locking object by tightening the encapsulation and therefore access to the most essential.

但为什么静态的?

private static readonly object Locker = new object();

在结束现场仅只是我的类中使用,同时我也可以只使用这个代替:

At the end the field is only used within my class only, and I could also just use this instead:

private readonly object Locker = new object();

任何意见?

更新:

作为一个例子,我已经粘贴此code(只是一个例子)。我可以在此使用静态还是非静态的更衣室,并都将正常工作。考虑下面我的答案应该是相当确定这样我的柜子? (对不起,我有一个面试下周需要知道每一个细节:)

As an example I have pasted this code (just an example). I could use static or non-static locker on this and both would work fine. Considering the answer below I should be rather defining my locker like this? (Sorry I have an interview next week and need to know every detail :)

private readonly object Locker = new object();

这里是code:

And here is the code:

    private int _priceA;
    private int _priceB;
    private EventWaitHandle[] _waithandle;
    private readonly IService _service;

//ctor
public ModuleAViewModel(IService service)
    {
        _service = service;
        _modelA = new ModelA();
        _waithandle = new ManualResetEvent[2];
        _waithandle[0] = new ManualResetEvent(false);
        _waithandle[1] = new ManualResetEvent(false);
        LoadDataByThread();
    }


 private void LoadDataByThread()
        {
            new Thread(() =>
                           {
                               new Thread(() =>
                               {
                                   lock (Locker)
                                   {
                                       _priceA = _service.GetPriceA();
                                   }
                                   _waithandle[0].Set();
                               }).Start();

                               new Thread(() =>
                               {
                                   lock (Locker)
                                   {
                                       _priceB = _service.GetPriceB();
                                   }
                                   _waithandle[1].Set();
                               }).Start();

                               WaitHandle.WaitAll(_waithandle);
                               PriceA = _priceA;
                               PriceB = _priceB;
                           }).Start();
        }

感谢

推荐答案

它不是非常普遍的是使用一个私有静态只读目的,用于多线程锁定 - 更确切地说,它是通常使用的锁定的在适当/选择粒度。有时就是静态。更多的时候,海事组织,它不是 - 但的实例的依据

It isn't "very common to use a private static readonly object for locking in multi threading" - rather, it is common to use a lock at the appropriate / chosen granularity. Sometimes that is static. More often, IMO, it isn't - but is instance based.

您看到静态锁是一个全局高速缓存,或全球数据/单身延迟加载的主要时间。而在后者中,有这样做的更好的办法呢

The main time you see a static lock is for a global cache, or for deferred loading of global data / singletons. And in the latter, there are better ways of doing it anyway.

所以这真的取决于:用于在您的情况怎么样弹部队?难道保护的东西,是的本身的静态的?如果是这样,锁应该是静态的。如果保护所的实例的基础,那么IMO锁应的的进行基于实例的东西。

So it really depends: how is Locker used in your scenario? Is it protecting something that is itself static? If so, the lock should be static. If it is protecting something that is instance based, then IMO the lock should also be instance based.

这篇关于为什么锁对象必须是静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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