In-depth concurrency AQS 3

Source: Internet
Author: User
In-depth concurrency AQS focuses on lock application. Here, record the lock release process of reentrantlock.

I mentioned earlier that when the thread releases the lock, it will check whether there are threads waiting in the clh queue. In principle, it will first release the thread node after the header node.

For reentrantlock, there are two situations when the current thread releases the lock:

1. No congested thread node in the current clh queue

2. The current clh queue has a blocking thread Node

To wake up threads in the queue in this case, perform the following steps:
Step 1:
Execute the reentranlock tryrelease method first, which will reduce the current thread status by 1. If the current state value is 0, the exclusiveownerthread is set to null. Here, the reentranlock tryrelease method can be unlocked, because the release operation can only be performed by the thread that currently owns the lock.
Step 2:
Try to wake up threads in the queue.

When the lock release operation client calls reentrantlock's unlock, it will call sync. Release (1), and then sync will call the release (INT Arg) method of the parent class AQS. The call process is as follows:
Reentrantlock. Unlock () => sync. Release (1) => release (INT Arg) of AQS ).

Next, let's take a look at the specific details. Here, because the AQS method is finally called, we should first understand the release method of AQS. The 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;    }



As described above, the tryrelease (ARG) method is called first. This tryrelease (ARG) method is implemented in the nonfairsync class of the subclass.
Its role is to reduce the lock Persistence State by 1 as mentioned above. If the State is reduced to 0, the exclusiveownerthread is set to null.
The Code is as follows:
 protected final boolean tryRelease(int releases) {            int c = getState() - releases;            if (Thread.currentThread() != getExclusiveOwnerThread())                throw new IllegalMonitorStateException();            boolean free = false;            if (c == 0) {                free = true;                setExclusiveOwnerThread(null);            }            setState(c);            return free; }


Tryrelease does not use a lock.

Then, if tryrelease returns true, it indicates that the lock has been completely released, the release method returned to AQS:
public final boolean release(int arg) {        if (tryRelease(arg)) {            Node h = head;            if (h != null && h.waitStatus != 0)                unparkSuccessor(h);            return true;        }        return false;    }


In this case, the node operation in the release queue is performed in if (tryrelease (ARG. The header node is extracted first. Then, if the header node is not empty and Its waitstatus is not 0 (indicating that at least some threads appear in the queue), The unparksuccessor method is called to release the thread node.
The unparksuccessor method code is as follows:
 
private void unparkSuccessor(Node node) {        /*         * If status is negative (i.e., possibly needing signal) try         * to clear in anticipation of signalling.  It is OK if this         * fails or if status is changed by waiting thread.         */        int ws = node.waitStatus;        if (ws < 0)            compareAndSetWaitStatus(node, ws, 0);        /*         * Thread to unpark is held in successor, which is normally         * just the next node.  But if cancelled or apparently null,         * traverse backwards from tail to find the actual         * non-cancelled successor.         */        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;        }        if (s != null)            LockSupport.unpark(s.thread);    }


Here, the header node is set to 0 first (if the current header is not canceled or it is already 0 ).
Then, find the next node of the header from the queue, and execute locksupport. unpark (S. Thread); to release the lock.

Otherwise, a node not canceled, not the header node and not null will be found from the beginning of tail and released.


In this way, some lock release procedures have been resolved.

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.