简体   繁体   English

获取和释放 Java 监视器锁(同步块、重入锁等)是否需要上下文切换到内核空间?

[英]Does acquiring and releasing java monitor locks (synchronized blocks, reentrant locks etc) requires context switch to kernel space?

AFAIK, Every object in Java has a mark word. AFAIK,Java 中的每个对象都有一个标记词。 The first word (the mark word) is used for storing locking information, either through a flag if only one thread is acquiring the lock or pointing to a lock monitor object if there is contention between different threads, and in both the cases, compare and swap construct is used for acquiring the lock.第一个字(标记字)用于存储锁定信息,如果只有一个线程正在获取锁,则通过一个标志,或者如果不同线程之间存在争用,则通过一个标志指向一个锁监视器对象,在这两种情况下,比较和交换结构用于获取锁。

But according to this link - https://www.baeldung.com/lmax-disruptor-concurrency但根据此链接 - https://www.baeldung.com/lmax-disruptor-concurrency

To deal with the write contention, a queue often uses locks, which can cause a context switch to the kernel.为了处理写争用,队列通常使用锁,这会导致到内核的上下文切换。 When this happens the processor involved is likely to lose the data in its caches.发生这种情况时,所涉及的处理器可能会丢失其缓存中的数据。

What am I missing ?我错过了什么?

Neither, synchronized nor the standard Lock implementations, require a context switch into the kernel when locking uncontended or unlocking.在无竞争锁定或解锁时, synchronized和标准的Lock实现都不需要上下文切换到内核中。 These operations indeed boil down to an atomic cas or write.这些操作确实归结为原子 cas 或 write。

The performance critical aspect is the contention , ie when trying to acquire the monitor or lock and it's not available.性能关键方面是争用,即当尝试获取监视器或锁但它不可用时。 Waiting for the availability of the monitor or lock implies putting the thread into a waiting state and reactivating it when the resource became available.等待监视器或锁的可用性意味着将线程置于等待状态并在资源可用时重新激活它。 The performance impact is so large, that you don't need to worry about CPU caches at all.性能影响如此之大,您根本不需要担心 CPU 缓存。

For this reason, typical implementations perform some amount of spinning, rechecking the availability of the monitor or lock in a loop for some time, when there is a chance of becoming available in that time.出于这个原因,典型的实现会执行一定量的自旋,重新检查监视器的可用性或在循环中锁定一段时间,当有机会在那个时间变得可用时。 This is usually tied to the number of CPU cores.这通常与 CPU 内核数相关。 When the resource becomes available in that time, these costs can be avoided.当资源在那个时间变得可用时,可以避免这些成本。 This, however, usually requires the acquisition to be allowed to be unfair , as a spinning acquisition may overtake an already waiting thread.然而,这通常需要允许不公平的获取,因为旋转获取可能会超过已经等待的线程。

Note that the linked article says before your cited sentence:请注意,链接的文章在您引用的句子之前说:

Queues are typically always close to full or close to empty due to the differences in pace between consumers and producers.由于消费者和生产者之间的速度差异,队列通常总是接近满或接近空。

In such a scenario, the faster threads will sooner or later enter a condition wait, waiting for new space or new items in a queue, even when they acquired the lock without contention.在这种情况下,更快的线程迟早会进入条件等待,等待队列中的新空间或新项目,即使它们在没有争用的情况下获取了锁。 So in this specific scenario, the associated costs are indeed there and unavoidable when using a simple queue implementation.所以在这个特定的场景中,当使用简单的队列实现时,相关的成本确实存在并且是不可避免的。

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

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