Biased locking in java(java中的偏向锁定)
问题描述
我一直在阅读有关偏向锁定如何使用标志 -XX:+UseBiasedLocking
可以提高非竞争同步性能的文章.我找不到关于它的作用以及它如何提高性能的参考.
I keep reading about how biased locking, using the flag -XX:+UseBiasedLocking
, can improve the performance of un-contended synchronization. I couldn't find a reference to what it does and how it improves the performance.
谁能解释一下它到底是什么,或者可能指向一些可以解释的链接/资源?
Can anyone explain me what exactly it is or may be point me to some links/resources that explains??
推荐答案
本质上,如果您的对象仅被一个线程锁定,JVM 可以进行优化并将该对象偏置"到该线程,以便后续对象上的原子操作不会产生同步成本.我想这通常适用于过于保守的代码,这些代码在对象上执行锁定而不会将它们暴露给另一个线程.实际的同步开销只会在另一个线程尝试获取对象上的锁时才会启动.
Essentially, if your objects are locked only by one thread, the JVM can make an optimization and "bias" that object to that thread in such a way that subsequent atomic operations on the object incurs no synchronization cost. I suppose this is typically geared towards overly conservative code that performs locks on objects without ever exposing them to another thread. The actual synchronization overhead will only kick in once another thread tries to obtain a lock on the object.
在 Java 6 中默认开启.
It is on by default in Java 6.
-XX:+UseBiasedLocking启用一种用于提高非竞争同步性能的技术.对象偏向"于首先通过 monitorenter 字节码或同步方法调用获取其监视器的线程;该线程执行的后续与监视器相关的操作在多处理器机器上相对快得多.启用此标志后,某些具有大量非竞争同步的应用程序可能会获得显着的加速;尽管已尝试将负面影响降至最低,但某些具有某些锁定模式的应用程序可能会变慢.
-XX:+UseBiasedLocking Enables a technique for improving the performance of uncontended synchronization. An object is "biased" toward the thread which first acquires its monitor via a monitorenter bytecode or synchronized method invocation; subsequent monitor-related operations performed by that thread are relatively much faster on multiprocessor machines. Some applications with significant amounts of uncontended synchronization may attain significant speedups with this flag enabled; some applications with certain patterns of locking may see slowdowns, though attempts have been made to minimize the negative impact.
这篇关于java中的偏向锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java中的偏向锁定


- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01