Java concurrency to fully understand the bias lock upgrade to a lightweight lock

Source: Internet
Author: User

There are many on-line lock, light-weight lock article, but to the bias lock how to upgrade speak not clear, some articles are contradictory, after the JVM source code (biasedLocking.cpp) careful analysis and tracking, the basic upgrade process has a clear process, The upgrade process is now described as follows:

Because of the bias lock, when the object is locked, the corresponding identity is written to the object header, we first put the object header (officially called: Mark Word) as follows (borrowed the user's picture):

     

From the above picture, we can know that when the object is in a biased lock, Mark Word has a biased lock marked as 1, the lock flag bit is 01, the following is the analysis of the JVM source (biasedLocking.cpp) parsing of the biased lock escalation process (ignoring some details), in the example : Thread 1 currently has a biased lock object, and thread 2 is required to compete to a biased lock.

    1. Thread competition lock object;
    2. Determines whether the current object header is a biased lock;
    3. Determine if a thread with a biased lock 1 still exists;
    4. Thread 1 does not exist, the direct setting of the bias lock is identified as 0 (thread 1 executes, does not actively release the biased lock);
    5. Use CAs to replace biased lock thread ID for thread 2, lock not upgraded, still biased for lock;
    6. Thread 1 still exists, pausing thread 1;
    7. Set the lock flag bit to 00 (becomes lightweight lock), the bias lock is 0;
    8. Reads a bar from the idle monitor record of thread 1 and puts it in the current monitor record of thread 1;
    9. Update Mark Word to point mark Word to the pointer to the monitor record in thread 1;
    10. Continue execution of thread 1 code;
    11. Lock upgrade to lightweight lock;
    12. Thread 2 spins to get the lock object;
There is still a problem, that is, how to determine that thread 1 does not exist?                        After analyzing the JVM source code (THREAD.CPP), the following conclusions are obtained: (1) When the thread executes start, it writes itself to a thread_list, which is a linked structure with pre and next nodes; Corresponding source location: void Threads::add (javathread* p, bool Force_daemon) {
//The threads lock must is owned
at the
Assert_locked_or_safepoint (Threads_lock);
//
See the comment for this method in THREAD.HPP for its purpose and
//Why it's called here.
p->initialize_queues ();
P->set_next (_thread_list);
_thread_list = p;
_number_of_threads++;
oop threadobj = P->threadobj ();
bool Daemon = true;
//Bootstrapping problem:threadobj can is null for initial
//Javathread (or for threads attached via JNI)
if ((!force_daemon) && (threadobj = = NULL | |!java_lang_thread::is_daemon (threadobj))) {
_number_of_non_daemon_threads++;
daemon = false;
  }
p->set_safepoint_visible (TRUE);
Threadservice::add_thread (P, daemon);
//Possible GC point.
Events::log (P, "Thread added:" Intptr_format, p);
   }
(2) After the thread executes, it will clean itself out of the thread list (source location: Threads::remove (this)); So just determine if thread 1 is present in the thread list and judge the source code (located atbiasedLocking.cpp) are as follows:BOOL thread_is_alive = false; if (Requesting_thread = = biased_thread) {thread_is_alive = true;} else {
     for (javathread* cur_thread = Threads::first (); Cur_thread! = NULL; cur_thread = Cur_thread->next ()) {
if (Cur_thread = = biased_thread) {thread_is_alive = true;              Break }          }

Java concurrency to fully understand the bias lock upgrade to a 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.