java中的偏向锁定

Biased locking in java(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中的偏向锁定

基础教程推荐