java中的偏向锁
我一直在阅读有关使用标志 -XX:+UseBiasedLocking
的偏向锁定如何提高无竞争同步的性能的内容。我找不到关于它的作用以及它如何提高性能的参考。
任何人都可以向我解释它到底是什么,或者可以向我指出一些解释的链接/资源吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我一直在阅读有关使用标志 -XX:+UseBiasedLocking
的偏向锁定如何提高无竞争同步的性能的内容。我找不到关于它的作用以及它如何提高性能的参考。
任何人都可以向我解释它到底是什么,或者可以向我指出一些解释的链接/资源吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
本质上,如果您的对象仅由一个线程锁定,则 JVM 可以进行优化并将该对象“偏向”该线程,从而使对该对象的后续原子操作不会产生同步成本。我认为这通常适用于过于保守的代码,这些代码对对象执行锁定而不将它们暴露给另一个线程。仅当另一个线程尝试获取该对象的锁时,实际的同步开销才会出现。
它在 Java 6 中默认处于打开状态。
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.
It is on by default in Java 6.
这还不能回答你的问题吗?
http://www.oracle.com/technetwork/java/tuning -139912.html#section4.2.5
不过我认为您会发现它在 1.6 中默认处于打开状态。使用 PrintFlagsFinal 诊断选项查看有效标志是什么。如果您正在调查服务器应用程序,请确保指定 -server,因为标志可能不同:
http: //www.jroller.com/ethdsy/entry/print_all_jvm_flags
Does this not answer your questions?
http://www.oracle.com/technetwork/java/tuning-139912.html#section4.2.5
Though I think you'll find it's on by default in 1.6. Use the PrintFlagsFinal diagnostic option to see what the effective flags are. Make sure you specify -server if you're investigating for a server application because the flags can differ:
http://www.jroller.com/ethdsy/entry/print_all_jvm_flags
我自己一直想知道偏向锁。
然而,似乎java的偏向锁在intel的nehalem处理器上比普通锁慢,并且大概在nehalem以来的两代处理器上。请参阅 http://mechanical-sympathy.blogspot.com/2011 /11/java-lock-implementations.html
在这里 http:// www.azulsystems.com/blog/cliff/2011-11-16-a-short-conversation-on-biased-locking
这里还有更多信息https://blogs.oracle.com/dave/entry/biased_locking_in_hotspot
我一直希望有一些相对便宜的方法可以撤销对英特尔的偏见锁定,但我开始相信这是不可能的。我看过的关于如何完成的文章依赖于以下任一方法:
I've been wondering about biased locks myself.
However it seems that java's biased locks are slower on intel's nehalem processors than normal locks, and presumably on the two generations of processors since nehalem. See http://mechanical-sympathy.blogspot.com/2011/11/java-lock-implementations.html
and here http://www.azulsystems.com/blog/cliff/2011-11-16-a-short-conversation-on-biased-locking
Also more information here https://blogs.oracle.com/dave/entry/biased_locking_in_hotspot
I've been hoping that there is some relatively cheap way to revoke a biased lock on intel, but I'm beginning to believe that isn't possible. The articles I've seen on how it's done rely on either:
值得一提的是,从 jdk15 开始,默认情况下将禁用偏向锁定
JEP 374:禁用并弃用偏向锁定
更远
是的,不再有
System.identityHashCode(o)
魔法了;)Worth mentioning that biased locking will be disabled by default jdk15 onwards
JEP 374 : Disable and Deprecate Biased Locking
Further
And ya, no more
System.identityHashCode(o)
magic ;)这里有两篇论文:
https://cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21 d-66fa-4885-92bf-c4e81c06d916/File/ccd39237cd4dc109d91786762fba41f0/qrl_oplocks_biasedlocking.pdf
https://www.oracle.com/technetwork/java/ biasedlocking-oopsla2006-wp-149958.pdf
网页:
https://blogs.oracle.com/dave/biased-locking-in- hotspot
jvm热点源码:
http://hg.openjdk.java.net/jdk8/jdk8/hotspot/file/87ee5ee27509/src/share/vm/oops/markOop.hpp
Two paper here:
https://cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/ccd39237cd4dc109d91786762fba41f0/qrl_oplocks_biasedlocking.pdf
https://www.oracle.com/technetwork/java/biasedlocking-oopsla2006-wp-149958.pdf
web page too:
https://blogs.oracle.com/dave/biased-locking-in-hotspot
jvm hotspot source code:
http://hg.openjdk.java.net/jdk8/jdk8/hotspot/file/87ee5ee27509/src/share/vm/oops/markOop.hpp