In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the basic execution process of AQS component". In daily operation, I believe many people have doubts about what is the basic execution process of AQS component. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "what is the basic execution process of AQS component"! Next, please follow the small series to learn together!
First of all, the basic execution process of AQS is to try to acquire the lock. If it succeeds, it will return. If it fails, it will enter the synchronization queue to wait for the lock resource. Based on this process, it can be seen that the queue and the nodes in the queue should be two key points.
First, let's look at the structure of queue nodes in AQS:
There are five fields in this class, and look at them in turn:
prev, next: points to its predecessor node and successor node, thus it can be seen that the synchronization queue in AQS is a doubly linked list.
Thread: The current thread object.
3.waitStatus: The status of the current node. It is an int variable, which has the following types in turn:
-1 SIGNAL The successor of the current node is blocked, so the current node needs to wake up its successor when it releases or cancels.
CANCELLED The current node is cancelled due to timeout or interrupt, and the node will remain unchanged after entering this state. Note: This is the only value greater than 0, and many decision logics use this feature.
-2 CONDITION The current node is in a conditional queue and cannot acquire a lock until the condition is met.
-3 PROPAGATE The lock information acquired by the current node needs to be passed to subsequent nodes. The shared lock mode uses this value.
nextWaiter: This value points to a SHARE node if the current node is in shared mode. If the current node is in the condition queue, this value points to the next node waiting for the condition.
After understanding the data structure of the Node node, look at the structure of the synchronization queue in exclusive lock mode:
Note: The head node is a new node that comes out of new, and the tail is directly pointing to the last node in the queue.
After understanding the exclusive lock mode queue, look at the synchronization queue in shared lock mode (note the differences):
Note: Shared locks and exclusive locks are the same synchronization queue, that is, nodes in the synchronization queue can be either shared or exclusive.
In addition to the synchronization queue used by exclusive locks and shared locks, there is also a very important queue that is the conditional queue. Let's take a look:
Note the difference between conditional queue and synchronous queue: 1. Head-tail pointer, 2. Single linked list
After understanding these basic data structures in AQS, finally look at the API provided by AQS:
Exclusive lock mode:
acquire exclusive mode acquires locks, does not respond to interrupts, and only sets the interrupt state of the current thread to true if an interrupt occurs
acquireInterruptibly exclusive mode acquires locks, and if the thread is interrupted during the acquisition of locks, an interrupt exception is thrown directly.
release lock resource
Shared lock mode:
acquireShared mode Acquires locks without responding to interrupts. If an interrupt occurs, it will only set the interrupt status of the current thread to true acquireSharedInterruptibly mode Acquires locks. If the thread is interrupted during the process of acquiring locks, it will directly throw an interrupt exception releaseShared releases lock resources.
Condition Queue:
await blocking wait condition, throw interrupt exception if interrupted
awaitUninterruptibly blocking wait condition, does not respond to interrupts, if an interrupt occurs only sets the thread interrupt state to true
awaitNanos waits nanoseconds and throws an interrupt exception if interrupted
awaitUntil Wait until a deadline, throw an interrupt exception if interrupted
await(long time, TimeUnit unit) Waits for a specified period of time and throws an interrupt exception if interrupted
signal Wakes up the first node in the waiting queue
signalAll Wakes up all nodes in the waiting queue
Extended API:
tryAcquire attempts to acquire an exclusive lock without blocking
tryAcquireNanos attempts to acquire an exclusive lock within the specified nanosecond time, and throws an interrupt exception if interrupted
tryRelease attempts to release an exclusive lock without blocking
tryAcquireShared tries to acquire a shared lock without blocking
tryAcquireSharedNanos attempts to acquire a shared lock within the specified nanosecond time and throws an interrupt exception if interrupted
tryReleaseShared attempts to release shared locks without blocking
At this point, the study of "what is the basic execution process of AQS components" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.