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

How to use Java concurrent queue BlockingQueue

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces Java concurrent queue BlockingQueue 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 everyone read this Java concurrent queue BlockingQueue how to use the article will have some gains, let's take a look at it.

BlockingQueue

First of all, the most basic, BlockingQueue is a first-in-first-out queue (Queue), why say blocking (Blocking) it? Because BlockingQueue supports when getting queue elements but the queue is empty, it will block and wait for elements in the queue to return; it also supports when adding elements, if the queue is full, then wait until the queue can put new elements.

BlockingQueue is an interface that inherits from Queue, so its implementation class can also be used as an implementation of Queue, which in turn inherits from the Collection interface.

BlockingQueue provides four different methods for insert, remove, and fetch operations in different scenarios: 1. throw an exception;2. return a special value (null or true/false, depending on the operation);3. block waiting for the operation until it succeeds;4. block waiting for the operation until it succeeds or timeout for a specified time. In summary:

Throws exception

Special value

Blocks

Times out

Insertadd(e)offer(e)put(e)offer(e, time, unit)Removeremove()poll()take()poll(time, unit)Examineelement()peek()

not applicable

not applicable

Each implementation of BlockingQueue follows these rules, of course, we do not have to memorize this table, know that there is such a thing, and then write the code according to their own needs to see the comments of the method to choose the appropriate method.

For BlockingQueue, our focus should be on the two methods put(e) and take(), since they are blocking.

BlockingQueue does not accept null inserts, and the corresponding method throws a NullPointerException when it encounters null inserts. A null value is usually used here as a special value returned (third column in the table), representing a poll failure. So, if null values are allowed to be inserted, then null cannot be used well to determine whether it represents failure or whether the value obtained is null.

A BlockingQueue may be bounded, and if the queue is found full at insert time, then the put operation will block. Usually, when we say unbounded queue here, we don't mean really unbounded, but its capacity is Integer.MAX_VALUE (more than 2.1 billion).

BlockingQueue is designed to implement producer-consumer queues, of course, you can also use it as a normal Collection, as mentioned earlier, it implements the java.util.Collection interface. For example, we can use remove(x) to delete any element, but this type of operation is usually not efficient, so try to use it only in a few cases, such as when a message has been queued but needs to be cancelled.

BlockingQueue implementations are thread-safe, but batch collection operations such as addAll, containsAll, retenAll, and removeAll are not necessarily atomic operations. For example, addAll(c) may throw an exception halfway after adding some elements. At this time, some elements have been added to BlockingQueue. This is allowed, depending on the specific implementation.

BlockingQueue does not support close or shutdown operations, because developers may want no new elements to be added to it, depending on the implementation and not enforced.

Finally, BlockingQueue supports multiple consumers and multiple producers in the producer-consumer scenario, which is actually a thread safety issue.

BlockingQueue is a relatively simple thread-safe container. Below I will analyze its specific implementation in JDK. It is time for Doug Lea to perform again.

ArrayBlockingQueue

ArrayBlockingQueue is a bounded queue implementation class of BlockingQueue interface, and the bottom layer is implemented by array.

Concurrency control is controlled by reentrant locks, and no matter insert operation or read operation, lock must be obtained before operation can be carried out.

ArrayBlockingQueue has the following properties:

//array used to store elements final Object[] items;//position of next read operation int takeIndex;//position of next write operation int putIndex;//number of elements in queue int count;//following are synchronizer used to control concurrency final ReentrantLock;private final Condition notEmpty;private final Condition notFull;

We use a schematic diagram to describe its synchronization mechanism:

Okay, PriorityBlockingQueue, we're done here.

About "Java concurrent queue BlockingQueue how to use" the content of this article is introduced here, thank you for reading! I believe that everyone has a certain understanding of the knowledge of "Java Concurrent Queue BlockingQueue How to Use". If you still want to learn more knowledge, please pay attention to 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

Development

Wechat

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

12
Report