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

What are the knowledge points of ArrayBlockingQueue?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you what are the knowledge points about ArrayBlockingQueue. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

JDK1.8 version, collate the knowledge points about ArrayBlockingQueue, and analyze the main methods

Feature 1. Bounded queue (implemented based on array) 2. First in, first out 3. If the queue is full, the put operation blocks; if the queue is empty, the take operation blocks. 4. Support fair or unfair policies (because ReentrantLock is used internally to implement blocking), fair policies (reduce throughput) 5. The classical double-conditional algorithm is used to guarantee the blocking function 6. Write data and consumption data use the same lock (which causes write data and consumption data not to be truly concurrent) the main methods 1.add (), offer (), put () method

All three are insert methods. If the queue is full in add (), an exception will be thrown; offer (), if the queue is full, return the false;put method, and if the queue is full, block waiting

Add () method: public boolean add (E e) {return super.add (e); / / calls the add () of the parent class, and the offer () method is called in the parent class, so the offer () method} offer () method public boolean offer (E) {checkNotNull (e) is finally called; / / determine whether the element is empty final ReentrantLock lock = this.lock; lock.lock () / / acquire the lock try {if (count = = items.length) / / determine whether the queue is full. If it is full, return false return false; else {enqueue (e) directly; / / put it in the queue return true;}} finally {lock.unlock () / / release lock}} put () method public void put (E) throws InterruptedException {checkNotNull (e); / / check whether the element is empty final ReentrantLock lock = this.lock; lock.lockInterruptibly (); / / acquire lock try {while (count = = items.length) / / determine whether the queue is full, and block the current thread notFull.await () if it is full / / wait for enqueue (e); / / insert queue} finally {lock.unlock (); / / release lock}} 2.poll (), take (), peek () method

All three are elemental queuing methods.

1.peek () fetches the element, but does not delete it

2.poll () takes out the element and deletes it

3.take () fetches the element and deletes it, blocking the current operation if the queue is empty

1.peek () method public E peek () {final ReentrantLock lock = this.lock; lock.lock (); / / acquire lock try {return itemAt (takeIndex); / / acquire element} finally {lock.unlock (); / / release lock}} 2.poll () public E poll () {final ReentrantLock lock = this.lock Lock.lock (); / / acquire the lock try {return (count = = 0)? Null: dequeue (); / / get the element and delete the element} finally {lock.unlock (); / / release the lock}} 3.take () public E take () throws InterruptedException {final ReentrantLock lock = this.lock; lock.lockInterruptibly () / / acquire lock try {while (count = = 0) / / queue as null blocking notEmpty.await (); return dequeue (); / / get the element and delete} finally {lock.unlock (); / / release lock}} 3.remove () method

Remove () removes the element and removes it using a loop

1.remove () method public boolean remove (Object o) {if (o = = null) return false; / / Why can't an exception be thrown directly in this place final Object [] items = this.items; final ReentrantLock lock = this.lock; lock.lock (); / / acquire lock try {if (count > 0) {final int putIndex = this.putIndex Int I = takeIndex; do {/ / loop until you find the element if (o.equals (items [I])) {removeAt (I); return true to delete } if (+ + I = = items.length) I = 0;} while (I! = putIndex);} return false;} finally {lock.unlock () / / release the lock}} above are the ArrayBlockingQueue knowledge points that the editor shared for you. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, 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.

Share To

Internet Technology

Wechat

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

12
Report