Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How the java Synchronizer AQS Architecture releases locks and synchronization queues

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "java Synchronizer AQS architecture how to release locks and synchronization queues", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "java Synchronizer AQS architecture how to release locks and synchronization queues" this article.

Introduction language

There is too much content in AQS, so we are divided into two chapters. Students who have not seen the first half of AQS can look back, ha. The first half of the chapter talked about a lot of basic concepts of locks, basic attributes, how to obtain locks, and so on. In this chapter, we mainly talk about how to release locks and synchronization queues.

1. Release the lock

The trigger time to release the lock is our commonly used Lock.unLock () method, which is designed to allow the thread to release access to the resource (see the overall architecture diagram purple route for the flow).

Release locks are also divided into two categories, one is the release of exclusive locks, the other is the release of shared locks, let's take a look at them respectively.

1.1. release the exclusive lock release

The release of the exclusive lock is relatively simple. Start from the head of the queue and find its next node. If the next node is empty, it will start at the end, always find the node whose status is not canceled, and then release the node. The source code is as follows:

/ / the basic method of unlock public final boolean release (int arg) {/ / tryRelease is handed over to the implementation class to implement, which is usually subtracted from the current synchronizer state by arg. If true is returned, the lock is released successfully. If (tryRelease (arg)) {Node h = head; / / header node is not empty, and the uninitialized state if (h! = null & & h.waitStatus! = 0) / / wakes up the node waiting for lock unparkSuccessor (h) from scratch; return true;} return false } / / interesting method: when the thread releases the lock successfully, the node in the synchronization queue will be awakened from node / / through the wake-up mechanism to ensure that the thread will not always block waiting in the synchronization queue for private void unparkSuccessor (Node node) {/ / node node is the node that currently releases the lock, and is also the head node of the synchronization queue int ws = node.waitStatus / / if the node has been cancelled, set the state of the node to initialize if (ws

< 0) compareAndSetWaitStatus(node, ws, 0); // 拿出 node 节点的后面一个节点 Node s = node.next; // s 为空,表示 node 的后一个节点为空 // s.waitStatus 大于0,代表 s 节点已经被取消了 // 遇到以上这两种情况,就从队尾开始,向前遍历,找到第一个 waitStatus 字段不是被取消的 if (s == null || s.waitStatus >

0) {s = null; / / there is a reason to iterate from the end instead of from the beginning. / / mainly because when the node is blocked, it is blocked in the acquireQueued method, and when it wakes up, it must be woken up in the acquireQueued method. The condition after waking up is to determine whether the front node of the current node is the head node. Here, the front node of the current node is judged, so the iterative order from tail to head must be used here, in order to filter out invalid front nodes. Otherwise, when the node is awakened, it is found that the front node is still an invalid node, and it will be blocked again. For (Node t = tail; t! = null & & t! = node; t = t.prev) / / t.waitStatus

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report