In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the Java concurrent programming LinkedBlockingQueue queue how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Java concurrent programming LinkedBlockingQueue queue how to use the article will have a harvest, let's take a look at it.
LinkedBlockingQueue is also implemented using an one-way linked list, which also has two Node to store the header and tail nodes, and an atomic variable count with an initial value of 0 to record the number of queue elements. There are also two instances of ReentrantLock, which are used to control the atomicity of elements in and out of the queue, in which takeLock is used to control that only one thread can get elements from the queue header, other threads must wait, putLock control can only have one thread to acquire locks at the same time, add elements at the end of the queue, and other threads must wait. In addition, notEmpty and notFull are conditional variables, and each of them has a conditional queue to store threads that are blocked when entering and leaving the queue, which is actually a producer-consumer model. The following is the code to create the exclusive lock.
Private final AtomicInteger count = new AtomicInteger (); / * Lock held by take, poll, etc * / private final ReentrantLock takeLock = new ReentrantLock (); / * * Wait queue for waiting takes * / private final Condition notEmpty = takeLock.newCondition (); / * * Lock held by put, offer, etc * / private final ReentrantLock putLock = new ReentrantLock (); / * Wait queue for waiting puts * / private final Condition notFull = putLock.newCondition ()
When the calling thread performs take, poll and other operations on the LinkedBlockingQueue instance, it needs to acquire the takeLock lock, thus ensuring that only one thread can operate on the chain header node at the same time. In addition, because the maintenance of the conditional queue inside the condition variable notEmpty uses the lock state management mechanism of takeLock, the calling thread must acquire the takeLock lock before calling the await and signal methods of notEmpty, otherwise an IllegalMonitorStateException exception will be thrown. A conditional queue is maintained inside the notEmpty. When the thread acquires the takeLock lock and calls the await method of notEmpty, the calling thread is blocked, and then the thread is placed in the conditional queue inside the notEmpty to wait until a thread calls the signal method of notEmpty.
When performing put, offer, and other operations on a LinkedBlockingQueue instance, you need to acquire a putLock lock to ensure that only one thread can operate on the tail node of the linked list at the same time. Similarly, because the maintenance of the conditional queue within the condition variable notFull uses the lock state management mechanism of putLock, the calling thread must acquire the putLock lock before calling the await and signal methods of notFull, otherwise an IllegalMonitorStateException exception will be thrown. A conditional queue is maintained inside the notFull. When the thread acquires the putLock lock and calls the await method of notFull, the calling thread is blocked, and then the thread is placed in the conditional queue inside the notFull to wait until a thread calls the signal method of notFull. The following is the code for the no-parameter constructor for LinkedBlockingQueue.
The following is the no-parameter construction code for LinkedBlockingQueue
Public static final int MAX_VALUE = 0x7ffffffffled public LinkedBlockingQueue () {this (Integer.MAX_VALUE);} public LinkedBlockingQueue (int capacity) {if (capacity = 0;}
Code (2) determines that if the current queue is full, the current element is discarded and false is returned.
Code (3) acquires the putLock lock, and after the current thread acquires the lock, other threads calling put and offer will be blocked (the blocked thread is placed in the AQS blocking queue of the putLock lock).
Code (4) redetermines whether the current queue is full, because other threads may have added new elements to the queue through put or offer operations during the execution of code (2) and the acquisition of the putLock lock. If the rejudgment queue is really dissatisfied, the new element joins the queue and increments the counter.
Code (5) determines that if there is still free space in the queue after the new element is queued, wake up a thread that is blocked in the conditional queue of notFull because of the call to notFull's await operation (such as the execution of the put method and the queue is full). Because the queue is now free, you can wake up an enlistment thread in advance.
Code (6) releases the acquired putLock lock. Note here that the lock release must be done in the finally because even if the try block throws an exception, the finally will be executed. In addition, after the lock is released, other threads blocked by calling the put operation will have one to acquire the lock.
The c0 in code (7) indicates that there is at least one element in the queue when code (6) releases the lock, and if there is an element in the queue, the signalNotEmpty operation is performed.
This is the end of the article on "how to use LinkedBlockingQueue queues for Java concurrent programming". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use LinkedBlockingQueue queues for Java concurrent programming". If you want to learn more, you are welcome to follow the industry information channel.
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.