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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to operate LinkedBlockingQueue put, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
JDK provides seven blocking queues, which are commonly used to implement producers and consumers, and LinkedBlockingQueue is one of the most commonly used.
Public void put (E e) throws InterruptedException {if (e = null) throw new NullPointerException (); int c =-1; Node node = new Node (e); final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; / / interruptible lock acquisition operation, that is, when the thread is in the Blocked state because the lock is not acquired, the thread can be interrupted and no longer wait. PutLock.lockInterruptibly (); try {/ / if the number of elements in the queue is equal to the queue capacity, that is, the queue is full, the current thread needs to wait. / / the reason for using the while loop is that after the thread is awakened, it is still necessary to determine whether the queue is full. / / that is, only if the queue is not satisfied can the current thread add the element while (count.get () = = capacity) {notFull.await ();} / / join the queue, that is, add the element enqueue (node); c = count.getAndIncrement () If (c + 1 < capacity) / / notify other producers notFull.signal ();} finally {putLock.unlock ();} if (c = = 0) / / notify consumers signalNotEmpty ();}
Summarize the process of adding operation.
1. Get putLock lock 2. If the queue is full, wait for (notFull.await ()) 3. The element joins the team 4. If the queue is not full after the current producer adds the element, notify other producers to add the element (notFull.signal ()) 5. Release putLock lock 6. If there are already elements in the queue, notify the consumer
The first thing is to acquire the lock. After getting the lock, wait if the queue is full, and add elements if the queue is dissatisfied.
After adding the element, notify other producers if the queue is not full
After adding an element, notify the consumer if the queue already has an element
On how to carry out the put operation of LinkedBlockingQueue to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.