是否有任何 JVM 使用自旋等待实现阻塞?

在 Java 并发实践中,作者写道:

In Java Concurrency in Practice, the authors write:

当锁被争用时,失败的线程必须阻塞.JVM 可以通过 spin-waiting(反复尝试获取锁直到成功)或通过 挂起被阻塞的线程来实现阻塞要么操作系统.哪个更高效取决于上下文切换开销和直到锁可用的时间之间的关系;spin-waiting 是短等待的首选,而暂停是长等待的首选.一些 JVM 会根据过去等待时间的分析数据自适应地在两者之间进行选择,但大多数 JVM 只是挂起等待锁定的线程.

When locking is contended, the losing thread(s) must block. The JVM can implement blocking either via spin-waiting (repeatedly trying to acquire the lock until it succeeds) or by suspending the blocked thread through the operating system. Which is more efficient depends on the relationship between context switch overhead and the time until the lock becomes available; spin-waiting is preferred for short waits and suspension is preferable for long waits. Some JVMs choose between the two adaptively based on profiling data of past wait times, but most just suspend threads waiting for a lock.

当我读到这篇文章时,我感到非常惊讶.由于分析结果,是否有任何已知的 JVM 在始终旋转等待或有时旋转等待时实现阻塞?现在很难相信.

When I read this I was quite surprised. Are there any known JVMs implementing blocking either on always spin-waiting or sometimes spin-waiting due to profiling results? It's hard to believe for now.

推荐答案

这里有证据表明 JRockit 可以使用自旋锁 - http://forums.oracle.com/forums/thread.jspa?threadID=816625&tstart=494

Here is evidence that JRockit can use spinlocks - http://forums.oracle.com/forums/thread.jspa?threadID=816625&tstart=494

如果你搜索spin";在 here 列出的 JVM 选项中,您将查看在 Hotspot JVM 中使用/支持自旋锁的证据.

And if you search for "spin" in the JVM options listed here you will see evidence for the use of / support for spinlocks in Hotspot JVMs.

如果您想要一个当前示例,请查看src/hotspot/share/runtime/mutex.cpp";在 OpenJDK Java 11 源代码树中.

And if you want a current example, look at "src/hotspot/share/runtime/mutex.cpp" in the OpenJDK Java 11 source tree.

相关文章