我一直在阅读有关偏向锁定如何使用标志 -XX:+UseBiasedLocking
可以提高非竞争同步性能的文章。我找不到关于它的作用以及它如何提高性能的引用。
谁能解释一下它到底是什么,或者可能指向一些可以解释的链接/资源?
最佳答案
本质上,如果您的对象仅被一个线程锁定,则 JVM 可以进行优化并将该对象“偏置”到该线程,从而使对该对象的后续原子操作不会产生同步成本。我想这通常适用于过于保守的代码,这些代码在对象上执行锁定而不会将它们暴露给另一个线程。实际的同步开销只会在另一个线程尝试获取对象上的锁时才会启动。
在 Java 6 中默认开启。
-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中的偏向锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9439602/