The realization principle of synchronized-java the art reading notes of concurrent programming

Source: Internet
Author: User
Tags cas

1.synchronized the basis for synchronization

Each object in Java can be used as a lock, with 3 different manifestations.

1. For the normal synchronization method, the lock is the current instance object.

2. For the static synchronization method, the lock is the class object of the current classes.

3. For a synchronous method block, the lock is a configuration object inside the synchonized bracket.

When a thread attempts to access a synchronous block of code, it must first get a lock, exit or throw an exception when it must release the lock. So where does the lock really exist?

From the JVM specification, you can see how synchonized is implemented in the JVM, and the JVM implements method synchronization and code synchronization based on entering and exiting the monitor object, but the implementation details are different. After the Synchronized keyword is compiled, the Montorenter and monitorexit Two bytecode instructions are added before and after the synchronization block, and the two bytecode instructions require a reference that points to the lock and unlock object. If a synchronized object is specified reference points to the object, if the method is decorated, if the class method points to the class object, the instance method points to the instance.

2.Java Object Header

The lock exists in the Java object header. If the object is an array type, the virtual machine stores the object header with 3 word widths, and if the object is a non-array type, the object header is stored with a 2-word width. In a 32-bit virtual machine, the word width equals four bytes, or 32bit.

In the Java object header, Mark Word stores the object's hashcode, generational age, and lock tag bits.

The data stored in Mark Word during Operation changes as the lock flag bit changes. Mark Word may change to store the following 4 kinds of data: Lightweight lock, heavyweight lock,GC mark, biased lock

3.Upgrade of Locks

In order to reduce the performance cost of acquiring locks and unlocking locks, Java SE1.6 introduces "biased lock" and "lightweight lock", so there are four states in the Java SE1.6 Lock, no lock state, favor lock state, lightweight lock state and heavy lock state, it will gradually escalate with the competition situation. Locks can be upgraded but not degraded, which means that a biased lock cannot be downgraded to a biased lock after it has been upgraded to a lightweight lock. This lock escalation strategy is not degraded, and is designed to improve the efficiency of acquiring locks and releasing locks.

4. Bias Lock

in order to reduce the performance cost of acquiring locks and unlocking locks, Java SE1.6 introduces the concept of "biased lock" and "lightweight lock", with Java SE1.6 having 4 states, no lock state, biased lock state, lightweight lock state, and heavy lock state, They will escalate as the competition progresses, and locks can be upgraded but not degraded.

when the lock object is fetched by the thread for the first time, the virtual machine sets the identity bit of the object header to "01", which can bias the mode, if Mark Word is set to the ID of the current thread through CAS operation, if the CAS operation succeeds, so that each time the thread enters this lock's synchronization block, The virtual machine is no longer doing anything. Once there are other threads competing for this lock, the bias mode is declared closed.

5.Lightweight lock

Lightweight Lock-Lock: Before the thread executes the synchronization block, the JVM creates space in the stack frame of the current thread to store the lock record and copies Mark word in the object header to the lock record, officially known as displaced Mark Word. The thread then attempts to replace Mark Word in the object header with a pointer to the lock record using CAs. If successful, the current thread acquires the lock and, if it fails, indicates that another thread is competing for the lock, and the current thread attempts to use the spin to acquire the lock.

Lightweight lock Unlock: When lightweight unlocked, atomic CAS operations are used to replace displaced Mark word back to the object header, and if successful, it means no contention occurs. If it fails, indicating that the current lock is competing, the lock expands into a heavyweight lock.

The realization principle of synchronized-java the art reading notes of concurrent programming

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.