What is AbstractQueuedSynchronizer?
This article introduces the knowledge of "what is AbstractQueuedSynchronizer". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
AbstractQueuedSynchronizer Abstract Class
Implement the following methods alone in the subclass
* {@ link # tryAcquire} * {@ link # tryRelease} * {@ link # tryAcquireShared} * {@ link # tryReleaseShared} * {@ link # isHeldExclusively}
State status modification uses the following methods
{@ link # getState}, {@ link* # setState} {@ link # compareAndSetState}:
Node {
/ / indicates the node status, CANCELLED. A value of 1 indicates that the current thread is canceled. The SIGNAL value is-1.
/ / indicates that the successor node of the current node contains threads that need to be run, that is, unpark
/ / the condition value is-2, which means that the current node is waiting for condition, that is, condition queue.
/ / propagate. The value is-3, which means that subsequent acquireShared in the current scene can be executed.
/ / the value is 0, indicating that the current node is in the sync queue, waiting to acquire the lock
Int waitStatus
/ / precursor node. If you cancel the current node, you need to complete the connection.
Node prev
/ / successor node
Node next
/ / Store the successor nodes in the condition queue
Node nextWaiter
/ / the current thread queued
Thread thread
}
/ / get an exclusive lock
While (acquire Lock) {
If (obtained) {
Exit the while loop
} else {
If (the current thread is not queued) {
Then join the queue.
}
Block the current thread
}
}
}
}
/ / release an exclusive lock
If (released successfully) {
Delete header node
Activate the successor node of the original header node
}
That's all for "what AbstractQueuedSynchronizer is". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!