Condition await-signal process details, javaconditionawait

Source: Internet
Author: User

Condition await-signal process details, javaconditionawait

Reprinted please indicate the source: http://blog.csdn.net/luonanqin

 

 

In the previous article, we talked about the lock-unlock process of ReentrantLock. Today we are talking about the await-signal process of Condition.

 

Condition class diagram:

 

  • The Condition Interface contains multiple await methods and two notification methods.
  • ConditionObject implements the Condition interface, which is an internal class of AbstractQueuedSynchronizer.
  • The newCondition method of Reentrantlock returns the Condition object related to a lock instance.

 

Like the release queue, the Condition queue is also a virtual queue, and each Node is associated through nextWaiter. Because the Condition Node must be changed to release Node, the blocking can be lifted, so prevWaiter is not required, which is described below.

The general process is:

All threads that call await will enter a Condition queue. Every time the signal thread is called, it will start from firstWaiter to find the uncanceled Condition Node and put it in the release queue, then, call the signal thread to execute the release method when await or unlock. Compared with lock-unlock, the normal process is simpler, but the interrupt processing is more complicated.

 

Let's take a look at the callAwait ()Blocking Process

 

The process can be divided into three steps:

Before blocking the current thread, determine whether the Condition Node is in the release queue. If there is no need to block it, You can directly participate in lock competition. The key code is as follows:
// Define actqueuedsynchronizer. conditionObject. class final boolean isOnSyncQueue (Node node) {// when entering the Condition queue, waitStatus must be CONDITION. If other threads call signal at the same time, the Node will be removed from the Condition queue, in addition, the CONDITION state is cleared during the removal. // From removal to entering the release queue, the prev must be null during this period, so false is returned, that is, if (node. waitStatus = Node. CONDITION | node. prev = null) return false; // when other threads enter the release queue, they will establish a forward and backward relationship with the previous Node. Therefore, if next exists, it indicates that if (node. next! = Null) // If has successor, it must be on queue return true;/** node. prev can be non-null, but not yet on queue because * the CAS to place it on queue can fail. so we have to * traverse from tail to make sure it actually made it. it * will always be near the tail in callto this method, and * unless the CAS failed (which is unlikely), it will be * there, so we hardly ever traverse much. * /// maybe the last Node entered the release queue, so it is tail, and its next must be null, so you need to search for return findNodeFromTail (node) from the end of the team );}

  

Signal ()The signal method of the flowchart is simpler, that is, from firstWaiter, locate a Node that has not been canceled and put it into the release queue. However, even if the Node found at the beginning is not canceled, it may also be canceled when it is in the queue, so the Code has done some special processing on this situation. I explained the Code as follows:
// Define actqueuedsynchronizer. conditionObject. class final boolean transferForSignal (Node node) {/** If cannot change waitStatus, the node has been canceled. * /// if the waitStatus change fails, it indicates that it has been canceled and there is no need to enter the release queue. Find a Condition Node for external recycling. // What if the waitStatus is successfully changed but then it is canceled? It doesn't matter. Although you have already entered the release queue, the unpark operation in the release method will skip the canceled Node. The check here is only to reduce unnecessary work when unpark if (! CompareAndSetWaitStatus (node, Node. CONDITION, 0) return false;/** Splice onto queue and try to set waitStatus of predecessor to * indicate that thread is (probably) waiting. if canceled or * attempt to set waitStatus fails, wake up to resync (in which * case the waitStatus can be transiently and harmlessly wrong ). * // p is the Node's precursor Node p = enq (node); int ws = p. waitStatus; // The effect is that you can set waitStatus only. When the thread is canceled, the cancelAcquire method will be called to set waitStatus to CANCEL, but it is not the if (ws> 0 |! CompareAndSetWaitStatus (p, ws, Node. SIGNAL) LockSupport. unpark (node. thread); return true ;}

  

We can see that the signal method only modifies the Node status and does not wake up the thread. To wake up the changed Node, call await () Again, or call unlock (). Both methods execute the release method internally to remove the Node blocking in the release queue. I have explained this in the previous article. Next I will call Await ()The process after the thread is removed from the blocking is also illustrated: The above is the detailed process of await and signal. SignalAll is similar to signal. Internally, all the nodes in the Condition queue are added to the release queue.

I will also use the flowchart to describe some Interrupt Processing in the future.

 

References:

How to Understand Condition http://www.liuinsect.com/2014/01/27/how_to_understand_condition/

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.