Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Biased locking in java

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??

like image 855
Chander Shivdasani Avatar asked Feb 24 '12 23:02

Chander Shivdasani


People also ask

What is bias in Java?

A position indicates a location between two characters. The bias can be used to indicate an interest toward one of the two sides of the position in boundary conditions where a simple offset is ambiguous.

What is locking mechanism in Java?

Java lock acts as thread synchronization mechanisms that are similar to the synchronized blocks. After some time, a new locking mechanism was introduced. It is very flexible and provides more options in comparison to the Synchronized block.

What is difference between lock and synchronized in Java?

Major difference between lock and synchronized: with locks, you can release and acquire the locks in any order. with synchronized, you can release the locks only in the order it was acquired.


2 Answers

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.

-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.

like image 96
Clement P Avatar answered Sep 18 '22 21:09

Clement P


Does this not answer your questions?

http://www.oracle.com/technetwork/java/tuning-139912.html#section4.2.5

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.

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

like image 35
Danny Thomas Avatar answered Sep 19 '22 21:09

Danny Thomas