Learn Java concurrent from source lock is how to maintain the internal thread queue

Learn Java concurrent from source lock is how to maintain the internal thread queue

In the previous article, Kaige synchronization component base frame - AbstractQueuedSynchronizer (AQS) made a general introduction. We know AQS can be accomplished by a built-in resource acquisition FIFO queue queue worker threads. So AQS is how to maintain this line work? Today we take a grilled steak AQS source. From source code to look at is how to maintain the right.

Benpian is: "Lock Series" tutorials "Kaige (Kaige Java kagejava) Concurrent Programming Learning" Series First: Under "Java and contract lock third in learning - learning from source Java Concurrency is how to maintain the internal thread queue. "

In Part I we know that there are internal AQS internal class -Node object. This object is to maintain the queue thread access to resources work. Specifically how to operate it? The main contents are: Node Node introduced; flowchart of how the queue is maintained in the synchronizer.

A: Node node object describes

AQS internal class within a Node objects. Let's look at what this object has attributes:

0fYtFv7v95U


After simplification:

Final the Node class {static 
       // wait state thread 
        volatile int waitStatus; 
        a node of the current node // 
        volatile PREV the Node; 
        // current node object 
        volatile Next the Node; 
        // current node maintains a thread object 
        volatile the Thread Thread; 
        // current the next (subsequent) node of 
        the node nextWaiter; 
 }


0fYtFvbBT72

Object property description

Int waitStatus:

Shows the state of the object which has four attributes:

static final int CANCELLED = 1: Thread canceled from the synchronization queue

static final int SIGNAL = -1: subsequent nodes wait state. After obtaining the current node resources, nodes need to disconnect and connect the follow-up before being released. After its release, will inform subsequent nodes, the post-solution continues to run.

static final int CONDITION = -2: the current node waiting. In the waiting condition notice. It may be understood as the condition queue.

static final int PROPAGATE = -3: in shared mode, the next unconditional propagation

0: default.

Node prev: on one node of the current node

Node Next: Current resolved subsequent nodes

Node nextWaiter: it can be understood as the type of node. It is shared or exclusive.

Thread thread: to get the current state of the thread synchronization objects.

FIG specifically as follows:

0fYtFwFpEIK


First, we need to understand that in the data structure, the structure is able to maintain FIFO queue mode. But there are individual queues and queue circular queue two kinds. Then, using a synchronizer which queue method?

From Node node attributes, we can see the properties before node and subsequent nodes. Instructions is a circular queue.

Two: to maintain a flow chart of the thread queue

To ensure the security thread synchronization provides several methods of CAS. As shown below:

0fYtFwtIZhg


CAS setting head node, a node, setting the set state, and the like provided tail node.

FIG operation flow can be summarized as follows:

0fYtFxBCei8


Flow Description:

Into the queue

Into the team process is as follows:

0fYtFxnWIEa


The process described in FIG:

When a plurality of threads simultaneously compete for resources, wherein a resource acquired thread (synchronous or lock state), this time to obtain the thread resources will be configured as a head node. Other lines of thread resources acquired nothing is configured as Node node object and placed into the queue. Node node is configured to thread the tail of the queue in the queue. In order to ensure thread safety, synchronization will CAS set an end node method (ie: compareAndSetTail ()) based to maintain the thread safety of this method need to pass the current thread, "he thinks," the tail node and the previous node, when the CAS after the success of the current node will formally establish diplomatic relations with the previous node. Node is arranged next node will point to the end of the head node.

The above figure 3 the thread and thread 1 will perform similar operations, the tail of the queue to add their own. This creates a complete two-way queuing up.

The queue

Dequeuing a flowchart as follows:

0fYtFyIvn9s


The team Process Description:

We can see from the flow chart of the team, all competing for resources concurrent threads are lined up. Synchronous queue follow FIFO (First In First Out). The first is to get the so-called node node synchronization status successfully. As to the first node in the thread synchronization status at the time of release, disconnects own relationship with the subsequent nodes, and then wake up the subsequent node operations. When the subsequent node acquiring synchronization status success, will set up their own, led by node, the original node first withdrew from the queue. If the original first node also needs to obtain it, will own thread configured Node node object, and then queued.

wx.jpg



Guess you like

Origin blog.51cto.com/kaigejava/2483896