In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the meaning of blocking queue". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's follow the editor's train of thought to study and learn what blocking queue means.
Blocking queue:
ArrayBlockingQueue: a bounded blocking queue consisting of array structures.
LinkedBlockingQueue: a bounded blocking queue consisting of linked list structures.
LinkedBlockingDeque: a bounded bidirectional blocking queue consisting of linked list structures.
LinkedTransferQueue: an unbounded blocking queue consisting of linked list structures.
SynchronousQueue: a blocking queue that does not store elements.
PriorityBlockingQueue: an unbounded blocking queue that supports prioritization.
DelayQueue: an unbounded blocking queue implemented using priority queues.
The blocking queue (BlockingQueue) is a queue that supports two additional operations. These two additional operations support blocking insertion and removal methods.
1) blocking insertion method is supported: this means that when the queue is full, the queue blocks the thread that inserts the element until the queue is dissatisfied.
2) blocking removal methods are supported: this means that when the queue is empty, the thread that gets the element will wait for the queue to become non-empty.
Blocking queues are often used in the scenarios of producers and consumers, where producers are threads that add elements to the queue, and consumers are threads that fetch elements from the queue. Blocking queues are containers that producers use to store elements and consumers use to get elements.
The following table shows some of the methods for blocking queues:
Method\ handling method throws an exception returns special value always blocks timeout exits insertion method add (e) offer (e) put (e) offer (eje timestamp unit) removal method remove () poll () take () poll (time,unit) check method element () peek () is not available
Throw an exception: when the blocking queue is full, inserting elements into the queue will throw an IllegalStateException ("Queue full") exception. When the queue column is empty, a NoSuchElementException exception is thrown when getting an element from the queue.
Returns a special value: the insert method returns whether it was successful, and if it succeeds, it returns true. Remove method, take an element out of the queue, and return null if not
Blocking: when the blocking queue is full, if the producer thread put the queue, the queue blocks the producer thread until it gets the data or exits in response to an interrupt. When the queue is empty, the consumer thread attempts to take elements from the queue, and the queue blocks the consumer thread until the queue is available.
Timeout exit: when the blocking queue is full, the queue will block the producer thread for a period of time, and if it exceeds a certain period of time, the producer thread will exit.
A blocking double-ended queue (BlockingDeque) is a blocking queue that supports inserting and fetching elements from both ends. It can be used if the production thread needs to be inserted at both ends of the queue and the consumption thread needs to be removed from both ends of the queue. It is mainly used in the producer-consumer model. In the multithreaded scenario, the producer adds elements at the end of the queue and the consumer consumes the elements at the head of the queue, thus isolating the production and consumption of the task.
LinkedBlockingQueue
LinkedBlockingQueue is a bounded blocking queue implemented with linked lists. The default and maximum length of this queue is Integer.MAX_VALUE. This queue sorts elements on a first-in-first-out basis.
PriorityBlockingQueue
PriorityBlockingQueue is an unbounded queue that supports priority. Elements are arranged in a natural order by default, or the collation of elements can be specified through the comparator comparator. The elements are arranged in ascending order.
ArrayListBlockingQueue
ArrayBlockingQueue is a bounded blocking queue implemented in an array. This queue sorts elements on a first-in, first-out (FIFO) basis. By default, visitors are not guaranteed fair access to the queue. The so-called fair access queue refers to all blocked producer or consumer threads. When the queue is available, you can access the queue in the order of blocking, that is, the producer thread that blocks first can insert elements into the queue first, and the consumer thread that blocks first can get the elements from the queue first. Usually, the throughput will be reduced in order to ensure fairness. We can create a fair blocking queue using the following code:
ArrayBlockingQueue fairQueue = new ArrayBlockingQueue (1000 million true)
The fairness of visitors is achieved using reentrant locks, as follows:
Public ArrayBlockingQueue (int capacity, boolean fair) {if (capacity 0) return 1; else if (sequenceNumber < x.sequenceNumber) return-1; else return 1;} long d = (getDelay (TimeUnit.NANOSECONDS)-other.getDelay (TimeUnit.NANOSECONDS)) Return (d = = 0)? 0: ((d < 0)-1: 1);}
How to implement the Delayed interface:
We can refer to the ScheduledFutureTask class in ScheduledThreadPoolExecutor. This class implements the Delayed interface. First of all, when the object is created, use time to record when the object can be used. The code is as follows:
ScheduledFutureTask (Runnable r, V result, long ns, long period) {super (r, result); this.time = ns; this.period = period; this.sequenceNumber = sequencer.getAndIncrement ();}
Then use getDelay to query how long the current element needs to be delayed, as shown in the following code:
Public long getDelay (TimeUnit unit) {return unit.convert (time-now (), TimeUnit.NANOSECONDS);}
Through the constructor, you can see that the unit of delay time parameter ns is nanosecond. It is best to use nanosecond when you design it, because you can specify any unit in getDelay. Once the unit is nanosecond, and the delay time is less than nanosecond, it will be troublesome. Note that getDelay returns a negative number when time is less than the current time.
How to implement a delay queue:
The implementation of the delay queue is simple: when the consumer gets the element from the queue, if the element does not reach the delay time, the current thread is blocked.
Long delay = first.getDelay (TimeUnit.NANOSECONDS); if (delay)
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: 253
*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.