Java Concurrency --Condition implementation analysis

Condition implementation analysis

ConditionObject synchronizer is AbstractQueuedSynchronizer inner classes, because the operation requires a Condition associated with acquiring the lock, so as an internal type synchronizer more reasonable. Each object contains a Condition queue (hereinafter, referred to as the waiting queue), the queue is critical to achieve the object wait Condition / notification function.

Waiting queue

Waiting queue is a FIFO queue , each node in the queue contains a reference to a thread, the thread is waiting on the object Condition threads, if a thread calls Condition.await () method, then the thread will release the lock, the node is configured to join the waiting queue and enters the wait state. In fact, the definition of the node definition multiplexed synchronizer of nodes , i.e., waits for the synchronous queue and queues static inner classes AbstractQueuedSynchronizer.Node node types are synchronizer.

A queue comprising a Condition, Condition has a first node (firstWaiter) and tail nodes (lastWaiter). The current thread calls Condition.await () method, the current thread will be configured node, the node from the tail and added to the waiting queue, waiting queue basic structure is shown below.

Here Insert Picture Description

As shown in FIG, Condition has reference end to end node and the new node only needs to point to the original tail node nextWaiter it, and updates to the end node. The above referenced node update process does not use the CAS guarantee, because calling await () method thread must be the thread that acquired the lock, that is to say the process is a lock to ensure thread-safe.

Object of the model on a monitor, the object has a synchronous queue and a wait queue, and the contract Lock (more precisely, the synchronizer) has a plurality of synchronous queue and waiting queue, which is the correspondence relationship as shown below .

Here Insert Picture Description

如图所示,Condition的实现是同步器的内部类,因此每个Condition实例都能够访问同步器提供的方法,相当于每个Condition都拥有所属同步器的引用。

等待

调用Condition的await()方法(或者以await开头的方法),会使当前线程进入等待队列并释放锁,同时线程状态变为等待状态。当从await()方法返回时,当前线程一定获取了Condition相关联的锁。

如果从队列(同步队列和等待队列)的角度看await()方法,当调用await()方法时,相当于同步队列的首节点(获取了锁的节点)移动到Condition的等待队列中。

ConditionObject的await方法:
Here Insert Picture Description

调用该方法的线程成功获取了锁的线程,也就是同步队列中的首节点,该方法会将当前线程构造成节点并加入等待队列中,然后释放同步状态,唤醒同步队列中的后继节点,然后当前线程会进入等待状态。

当等待队列中的节点被唤醒,则唤醒节点的线程开始尝试获取同步状态。如果不是通过其他线程调用Condition.signal()方法唤醒,而是对等待线程进行中断,则会抛出InterruptedException。

如果从队列的角度去看,当前线程加入Condition的等待队列,该过程如下图示。

Here Insert Picture Description
如图所示,同步队列的首节点并不会直接加入等待队列,而是通过addConditionWaiter()方法把当前线程构造成一个新的节点并将其加入等待队列中。

通知

调用Condition的signal()方法,将会唤醒在等待队列中等待时间最长的节点(首节点),在唤醒节点之前,会将节点移到同步队列中。

== ConditionObject的signal方法:==
Here Insert Picture Description
调用该方法的前置条件是当前线程必须获取了锁,可以看到signal()方法进行了isHeldExclusively()检查,也就是当前线程必须是获取了锁的线程。接着获取等待队列的首节点,将其移动到同步队列并使用LockSupport唤醒节点中的线程。

Node from the process wait queue to queue synchronization as shown below:
Here Insert Picture Description

By moving enq (Node node) method, the head node thread queue waiting call to the synchronizer safely synchronous queue. When the node moves to the sync queue, then the current thread using the thread wake LockSupport node.

After the thread wakes up, will await while cycling () method exit (isOnSyncQueue (Node node) method returns true, the node is already in sync queue), which in turn calls synchronizer acquireQueued () method is added to obtain the synchronization status competition.

After successfully acquiring synchronization status (or lock), thread to be awakened from a previous call await () method returns, this time the thread has successfully acquired the lock.

Condition of signalAll () method, each node is equivalent to wait in the queue are the implementation of a signal () method, the effect is to wait for all the nodes in the queue to move all synchronization queue, and wake up the thread of each node pair.

Published 635 original articles · won praise 1857 · Views 230,000 +

Guess you like

Origin blog.csdn.net/cold___play/article/details/104055654