java--bias Lock

Source: Internet
Author: User
Tags cas

Java bias Lock (biased Locking) is a multithreaded optimization introduced in Java 6. 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

A bias lock, as the name implies, is biased towards the first thread that accesses the lock.

If the lock is not accessed by another thread during the next run, the thread holding the biased lock will never need to trigger the 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. (A bias lock can only work on a single thread).

When the lock object is fetched by the thread for the first time, the virtual opportunity sets the flag bit in the Mardword of the object's head to 01, which is the bias mode. The ID of the thread is present in Markword, and after success, the thread holding the lock does not perform any synchronization operations. However, when another thread attempts to acquire the lock, this bias mode declares the end

Deepen reading;

  Deep JVM lock Mechanism 1-synchronized

  Deep JVM lock Mechanism 2-lock

java--bias Lock

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.