Java--Lightweight lock

Source: Internet
Author: User
Tags cas

Before you learn about lightweight locks, you first need to know what a CAs

Cas--compare and swap compare and swap--determine if the data is being updated by comparing the values in the old values passed in with the value in the original memory location.

The semantics of CAS is "I think the value of V should be a, if it is, then update the value of V to B, otherwise do not modify and tell the value of V is actually how much",cas is an optimistic locking technique, when multiple threads try to update the same variable simultaneously using CAS, Only one of the threads can update the value of the variable, and the other threads fail, and the failed thread is not suspended, but is told that the competition has failed and can try again.

The CAS directive requires three operands, the memory location (which can be understood in Java as the position of the variable in memory, the v identifier), the old expected value (identified with a), and the new value (identified with B). When the CAS instruction executes, when and only if v meets the expected value A, the processor will update V with the new value B, otherwise he will not perform the update, but if V is updated, the old value of V will be returned, and the sequence of operations is atomic.

1 intCompare_and_swap (int* Reg,intOldval,intnewval)2 {3 ATOMIC ();4   intOld_reg_val = *reg;5   if(Old_reg_val = =oldval)6*reg =newval;7 end_atomic ();8   returnOld_reg_val;9}

Details can be found in the following section: http://www.tuicool.com/articles/zuui6z

Baidu Encyclopedia of the above said can also understand its principle:

Compare and swap, a mechanism that solves the performance loss caused by the use of locks in multithreaded parallel situations, the CAS operation consists of three operands-the memory location (V), the expected original value (A), and the new value (B). If the value of the memory location matches the expected original value, the processor automatically updates the location value to the new value. Otherwise, the processor does nothing. In either case, it will return the value of that location before the CAS directive. CAS effectively explains that "I think position v should contain a value of a; if it is included, B is placed in this position; otherwise, do not change the position, just tell me the current value of this position."

In addition to understanding the lightweight lock, you also need to know markword---

This is where lock record is called the lock recording space, if the image above the PTR to lock record flag bit is 01, indicating that the object is not locked, then the virtual machine will first establish a lock record space in the current thread frame stack, A copy of the Markword that is used to store the current object, and then uses the CAS operation to attempt to update Mark-word to a pointer to the lock record, if the update succeeds then it means that the thread already has permission to use the object, then the PTR to lock The record's flag bit is set to 00 to prove that the object is in a lightweight lock state.

When the thread uses the object, the first thing to check is whether the flag bit is in a lightweight lock state, or if it proves that the object is being used by a wired thread.

Also in the process of unlocking, the CAs method is used, and he tries to replace the original mark Word in the lock record's Mard word with the object's head. If the substitution succeeds, the thread has already completed the synchronization and, if not, indicates that another thread has tried to acquire the lock, then it wakes up the suspended thread.

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