Java-"JUC" Reentrantlock release lock

Source: Internet
Author: User

Java Multithreading Series--"Juc lock" 04 Fair Lock (ii) Release Fair Lock (based on jdk1.7.0_40)

1. Unlock ()

Unlock () implemented in Reentrantlock.java, the source code is as follows:

public void Unlock () {    sync.release (1);}

Description :
Unlock () is an unlock function, which is implemented by Aqs's release () function.
Here, the meaning of "1" is the same as the "Get Lock function acquire (1)", which is the parameter that sets "state of release lock". Since the "fair lock" is reentrant, for the same thread, each time a lock is released, the state of the lock-1.

The relationship between Aqs, Reentrantlock and Sync is as follows:

public class Reentrantlock implements Lock, java.io.Serializable {    private final sync sync;    Abstract static class Sync extends Abstractqueuedsynchronizer {        ...    }    ...}

From this, we find that sync is a member object in Reentrantlock.java, and Sync is a subclass of Aqs.

2. Release ()

Release () implemented in Aqs, the source code is as follows:

Public final Boolean release (int arg) {    if (Tryrelease (ARG)) {        Node h = head;        if (h! = NULL && H.waitstatus! = 0)            unparksuccessor (h);        return true;    }    return false;}

Description :
Release () calls Tryrelease () to attempt to free the locks held by the current thread lock. If successful, it wakes the successor waiting thread and returns true. Otherwise, return false directly.

3. Tryrelease ()

Tryrelease () is implemented in the Reentrantlock.java sync class with the following source code:

Protected Final Boolean tryrelease (int releases) {    //C is the state after the release lock    int c = getState ()-releases;    If the "current thread" is not a "lock holder", an exception is thrown!    if (Thread.CurrentThread ()! = Getexclusiveownerthread ())        throw new Illegalmonitorstateexception ();    Boolean free = false;    If the "lock" has been completely freed by the current thread, the holder of the set "lock" is null, that is, the lock is a fetching state.    if (c = = 0) {Free        = true;        Setexclusiveownerthread (null);    }    Sets the state of the lock for the current thread.    setState (c);    return free;}

Description :
The role of Tryrelease () is to attempt to release the lock.
(01) Throws an exception if the "current thread" is not a "lock holder".
(02) If "Current thread" after this release lock operation, the owning state of the lock is 0 (that is, the current thread completely frees the "lock"), then the holder of the set "lock" is null, that is, the lock is the available state. Also, the status of the lock that updates the current thread is 0.
GetState (), SetState () has been introduced in the previous chapter and is no longer explained here.
Getexclusiveownerthread (), Setexclusiveownerthread () is defined in Aqs's parent class Abstractownablesynchronizer.java, and the source code is as follows:

Public abstract class Abstractownablesynchronizer    implements Java.io.Serializable {    //"lock" holding thread    private Transient Thread exclusiveownerthread;    Set "Hold thread for lock" for T    protected final void Setexclusiveownerthread (thread t) {        exclusiveownerthread = t;    }    Get "Hold Thread of Lock"    protected Final thread Getexclusiveownerthread () {        return exclusiveownerthread;    }       ...}

4. Unparksuccessor ()

The "Current thread" release lock succeeds in release (), which wakes up the successor thread of the current thread.
According to the FIFO rules of the CLH queue, the "current thread" (that is, the thread that has acquired the lock) must be head, and if the CLH queue is not empty, the next waiting thread for the lock is awakened.
Let's look at the source code of Unparksuccessor (), which is implemented in Aqs.

private void Unparksuccessor (node node) {    //Gets the current thread's state    int ws = Node.waitstatus;    If the status is <0, then the state =0 if    (WS < 0)        compareandsetwaitstatus (node, WS, 0);    Gets the "valid successor" of the current node, if it is not valid, is obtained through a for loop.    //Valid here refers to the "successor node corresponding to the thread state <=0"    Node s = node.next;    if (s = = NULL | | s.waitstatus > 0) {        s = null;        for (node t = tail; t! = null && t! = Node; t = t.prev)            if (t.waitstatus <= 0)                s = t;    }    Wake "thread corresponding to successor node"    if (s! = null)        Locksupport.unpark (s.thread);}

Description :
The role of Unparksuccessor () is to "wake the successor thread of the current thread." After the subsequent thread is awakened, the lock can be acquired and resumed running.
For instructions on Node.waitstatus, please refer to the "Introduction to Node Class" in the previous chapter.

Note: Unlock best put in finally!!!!!! Unlock better put it in the finally!!!!!! Unlock better put it in the finally!!!!!! (important thing to say three times)

Releasing an unfair lock (based on jdk1.7.0_40)

An unfair lock and a fair lock are the same method and strategy for releasing the lock.
In the front of the "Java Multithreading Series--" Juc lock "04 of the Fair Lock (ii)", "release of the Fair lock" has been introduced, here will not repeat the explanation.

Summarize

The process of "releasing a lock" is relatively simple for the process of "acquiring a lock". When a lock is released, the primary action is to update the state of the lock corresponding to the current thread. If the current thread is completely freed from the lock, the holding thread that sets the lock is null, sets the current thread's state to be empty, and then wakes up the successor thread.

Java-"JUC" Reentrantlock release 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.