Java Multithreading Series--"Juc lock" 04 Fair Lock (II)

Source: Internet
Author: User

Tag: Code member jdk1.7 read illegal boolean return port Owner

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 Implements Lock, java.io.Serializable {    privatefinal  sync sync;     Abstract Static class 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 " Span style= "COLOR: #0000ff" >final  boolean  release (int   Arg) { if   = 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 BooleanTryrelease (intreleases) {    //C is the state after the release of the lock    intc = getState ()-releases; //if the "current thread" is not a "lock holder", an exception is thrown!     if(Thread.CurrentThread ()! =Getexclusiveownerthread ())Throw Newillegalmonitorstateexception (); BooleanFree =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); returnFree ;}

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 classAbstractownablesynchronizerImplementsjava.io.Serializable {//hold thread for "lock"    Private transientThread Exclusiveownerthread; //set "lock holding thread" to T    protected Final voidSetexclusiveownerthread (Thread t) {Exclusiveownerthread=T; }    //get "Hold thread of lock"    protected FinalThread Getexclusiveownerthread () {returnExclusiveownerthread; }       ...}

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 voidUnparksuccessor (node node) {//gets the state of the current thread    intWS =Node.waitstatus; //If the status <0, the state is set =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. //This is valid, 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.

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.

Reference:

Http://www.cnblogs.com/skywang12345/p/3496609.html

Java Multithreading Series--"Juc lock" 04 Fair Lock (II)

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.