Java biased lock implementation principle (biased Locking)

Source: Internet
Author: User
Tags cas

Http://kenwublog.com/theory-of-java-biased-locking

Readers of this article need to have a sense of Java lightweight locks and know the names of lock record, Mark Word, and so on. You can refer to one of my blog post: Java Lightweight lock principle in detail (lightweight Locking)

Java bias Lock (biased Locking) is a multithreaded optimization introduced by JAVA6. It further improves the running performance of the program by eliminating the synchronization primitives in the case of non-competitive resources.

Lightweight locking is also a multi-threaded optimization, which differs from biased locking in that lightweight locks are passed through CAs to avoid costly mutex operations, while biased locking is a complete elimination of synchronization in a non-competitive scenario, even CAS is not executed (CAS itself is still an operating system synchronization primitive, Always go back and forth between the JVM and the OS for a certain amount of overhead).

The so-called non-competitive scenario, for example, is a single-threaded access to a resource or method with synchronization.

Principle of biased lock implementation

Bias Lock, as the name implies, it will favor the first access lock thread, if in the next run process, the lock is not accessed by other threads, then hold a biased lock thread will never need to trigger synchronization.
If another thread preemption lock is encountered during the run, the thread holding the biased lock is suspended, and the JVM tries to eliminate the biased lock on it and restore the lock to a standard lightweight lock. (Bias lock only works on single thread)

The biased lock can be understood more intuitively:

This diagram omits several steps related to lightweight locking, focusing more on the state change of the biased lock.
Bias mode and non-biased mode, in the following Mark Word table, is mainly reflected in whether the thread ID field is empty.

Suspends a thread that holds a biased lock, which operates like a GC pause, but differs in that it suspends only the thread that holds the biased lock (not the current thread).

As mentioned in the Orange area description of preemption mode, point to the nearest lock record in the current stack (in the lightweight lock principle article, the lock record is a memory space created on the stack before it enters the lock).
The most recent lock record mentioned here is actually the lock record assigned on the stack frame where the current lock is located.
The entire step is the process of recovering from a biased lock to a lightweight lock.

Biased locking also brings additional overhead

In JDK6, the bias lock is enabled by default. It improves the performance of single-threaded access synchronization resources.
But imagine that if your synchronization resources or code are always multi-threaded access, then the step to eliminate the bias lock is redundant for you. In fact, the cost of eliminating bias locks is pretty big.

So when you are very familiar with your own code, you can disable the bias lock-xx:-usebiasedlocking.

Java biased lock implementation principle (biased Locking)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.