简体   繁体   English

使用显式锁被认为是昂贵的吗?

[英]Is using explicit locks considered expensive?

It is mentioned that using an explicit lock declaration for object level lock as the one below is a good practice.提到使用如下所示的对象级锁的显式锁声明是一种很好的做法。 However, I think for every instance of the object that we create we will end up creating a new object for the lock and access our method via a lock which are both expensive steps.但是,我认为对于我们创建的对象的每个实例,我们最终都会为锁创建一个新对象并通过锁访问我们的方法,这两个步骤都是昂贵的。

However, in the case of synchronized method, I believe there isn't any such thing as you are using object's own lock and you only pay the cost of acquiring a lock.但是,在同步方法的情况下,我相信没有任何事情,因为您使用的是对象自己的锁,您只需支付获取锁的费用。 You avoid the creation of an explicit lock object.您避免创建显式锁定对象。

Am I missing anything here?我在这里错过了什么吗?

public class DemoClass
{
    private final Object lock = new Object();
    public void demoMethod(){
        synchronized (lock)
        {
            //other thread safe code
        }
    }
}

Is using explicit locks considered expensive?使用显式锁被认为是昂贵的吗?

Generally, no.一般来说,没有。 It is not considered expensive.它不被认为是昂贵的。 (Modulo that different people have different opinions, and different applications have different requirements.) (取模不同的人有不同的看法,不同的应用有不同的要求。)

The cost of a lock Object instance is about 8 bytes of memory plus the memory for the variable holding the reference to the lock in the parent object.锁定Object实例的成本约为 8 字节的内存加上用于保存对父对象中的锁定的引用的变量的内存。 That is 12 or 16 bytes in all.那总共是 12 或 16 个字节。

The cost of creating a lock Object instance is ... a few nanoseconds.创建一个锁定Object实例的成本是……几纳秒。 (Not exactly sure how many, but it is trivial.) (不完全确定有多少,但这是微不足道的。)

The cost of reclaiming a lock Object instance when it becomes garbage is typically zero.当一个锁Object实例变成垃圾时回收它的成本通常为零。 (The GC costs are incurred when the lock and its parent object are not garbage ...) (当锁及其父对象不是垃圾时,会产生 GC 成本......)

These costs are all insignificant unless you have millions of these objects or an excessive object turn-over rate or you have severe memory or latency constraints.这些成本都是微不足道的,除非您有数百万个这样的对象过高的对象周转率,或者您有严重的内存或延迟限制。

(Or if you have decided to put / use locks on things that don't need to be locked. But that is a different problem.) (或者,如果您决定在不需要锁定的事物上放置/使用锁定。但这是一个不同的问题。)

There is (AFAIK) minimal difference in the cost of acquiring or releasing a lock on an explicit lock object versus acquiring / releasing the lock on this . (AFAIK) 在显式锁对象上获取或释放锁的成本与在this上获取/释放锁的成本差异很小。 Maybe one memory access.也许是一次内存访问。


For a typical application these cost differences won't matter.对于典型应用,这些成本差异无关紧要。 There will be more important things to optimize.会有更重要的事情需要优化。 The standard advice applies:标准建议适用于:

  • Code it simple 1 to start with.编码简单1开始。 Simple is easier to write and debug ... and read.简单更容易编写和调试......和阅读。
  • Avoid optimizing too soon;避免过早优化; eg while you are writing the code.例如,在您编写代码时。
  • Benchmark and profile your application before you optimize.优化之前对您的应用程序进行基准测试和分析。
  • Use the results of the measurements to decide what is worth optimizing.使用测量结果来决定什么值得优化。

The performance issues probably pale into insignificance compared with the potential problems that can be caused by locking on this .性能问题可能是小巫见大巫与可以通过锁定对引起的潜在问题相比this In reality, it depends on your application complexity and implementation details.实际上,这取决于您的应用程序复杂性和实现细节。


1 - Simple enough for you and your co-workers. 1 - 对您和您的同事来说足够简单。 It is context dependent.它是上下文相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM