In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to master the blocking queue". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to master the blocking queue".
What is a queue?
Queue is a special first-in-first-out linear table, referred to as FIFO. The special thing is that it is only allowed to be inserted at one end and deleted at the other end.
The end of the insert operation is called the end of the queue, and the end of the delete operation is called the head of the line. When there are no elements in the queue, it is called an empty queue
Queues are widely used in programming, including some middleware whose underlying data structure is queues (the basic content is not explained too much)
What is a blocking queue?
The queue is just the queue. What the heck is the blocking queue?
A blocking queue is a queue that adds two additional operations to the queue, namely:
Plug-in method that supports blocking: when the queue capacity is full, the thread inserting the element will be blocked until the queue has extra capacity
Blocking removal method is supported: when there are no elements in the queue, the thread that removes the element is blocked until there are elements in the queue that can be removed
This article takes LinkedBlockingQueue as an example to describe the differences between queues. For the convenience of friends, LinkedBlockingQueue takes the alias LBQ.
Because it is a source code parsing article, it is recommended that you watch it on the PC side. Of course, if the screen is big enough, forget it.
Blocking queue inheritance relationship
Blocking queue is an abstract name. The underlying data structure of blocking queue can be array, one-way linked list, or two-way linked list.
LBQ is a queue made up of one-way linked lists. The following figure shows the inheritance diagram of LBQ.
As you can see from the figure, LBQ implements the BlockingQueue interface and BlockingQueue implements the Queue interface.
Queue interface analysis
Let's first analyze which methods are defined in a wave of Queue interfaces in a top-down manner.
/ / if the queue capacity allows, insert the element into the queue immediately, and return / / after success. If the queue capacity is full, an exception boolean add (E e) is thrown; / / if the queue capacity allows, immediately insert the element into the queue, and return / /? If the queue capacity is full, return false / / when does offer boolean offer (E e) more than the add method when using bounded queues; / / retrieve and delete the header node of the queue, and the return value is deleted queue header node / /? If the queue is empty, throw an exception E remove (); / / retrieve and delete the header node of the queue, and the return value is the deleted queue header node / /? Returns null E poll () if the queue is empty; / / checks but does not delete the queue header node / /? Throw an exception E element () if the queue is empty; / / check but do not delete the queue header node / /? Returns null E peek () if the queue is empty
Summarize the methods of the Queue interface, which can be divided into three categories:
Add elements to the queue container: add, offer
Remove elements from the queue container: remove, poll
Query whether the queue header node is empty: element, peek
Considering the program robustness of interface API, it can be divided into two categories:
Robust API:offer, poll, peek
Non-robust API:add, remove, element
The interface API is not robust to speak of. The robust limit here means that if a non-robust API interface is used, the program is a bit more likely to make mistakes, so we should pay more attention to how to catch possible exceptions and how to handle them.
BlockingQueue interface analysis
The BlockingQueue interface inherits from the Queue interface, so some API interfaces with the same semantics are not interpreted.
/ / insert the specified element into the queue, and if the queue is full, wait until space is available. You can interrupt / / while waiting through the throws exception. Compared with the Queue interface, it is a brand-new method void put (E) throws InterruptedException; / / to insert the specified element into the queue and, if the queue is full, to wait for the specified time to make room. According to the throws exception, you can interrupt / / while waiting. This is equivalent to the extension method boolean offer (E, long timeout, TimeUnit unit) throws InterruptedException; / / of offer (E e) to retrieve and remove the header node of the queue and, if necessary, wait until the element is available; know from the throws exception that E take () throws InterruptedException; / / can be interrupted while waiting and delete the header of the queue, and if necessary to make the element available, wait for the specified waiting time Know from the throws exception that you can interrupt / / while waiting. Equivalent to the extension method of poll () E poll (long timeout, TimeUnit unit) throws InterruptedException; / / returns the remaining capacity of the queue, or Integer.MAX_VALUE int remainingCapacity () if it is an unbounded queue; / / returns true public boolean contains (Object o) if the queue contains the specified elements; / / removes all available elements from this queue and adds them to the given collection int drainTo (Collection)
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.