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 PriorityBlockingQueue of JUC

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to use the PriorityBlockingQueue of JUC". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The bottom layer of PriorityBlockingQueue relies on arrays as the storage structure, and the maximum capacity limit is Integer.MAX_VALUE-8, so it can almost be regarded as unbounded. Like PriorityQueue, PriorityBlockingQueue also introduces a heap data structure to prioritize queue elements, using the minimum heap structure by default.

In addition, we can infer that PriorityBlockingQueue is a blocking queue from the word Blocking. PriorityBlockingQueue is implemented from the BlockingQueue interface and ensures thread safety based on ReentrantLock locks. It is important to note, however, that PriorityBlockingQueue blocking is only for out-of-queue operations, and threads that leave the queue when the queue is empty block waiting for other threads to add new elements to the queue. For queuing operations, because PriorityBlockingQueue is defined as unbounded, the thread executing the queue will get an immediate response. If the underlying array of the queue is full, the thread will try to expand the underlying array. When the underlying data reaches the capacity limit and cannot continue to expand, an OOM exception will be thrown.

Let's first take a look at the field definition of PriorityBlockingQueue, as follows:

Public class PriorityBlockingQueue extends AbstractQueue implements BlockingQueue, Serializable {/ * * default initial queue capacity * / private static final int DEFAULT_INITIAL_CAPACITY = 11; / * * queue capacity limit * * Some VMs reserve some header words in an array. * Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size exceeds VM limit * / private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE-8; / * an array of queue elements, organized according to the minimum heap * * Priority queue represented as a balanced binary heap: * the two children of queue [n] are queue [2*n+1] and queue [2 * (Numeric 1)]. * The priority queue is ordered by comparator, or by the elements' natural ordering, * if comparator is null: For each node n in the heap and each descendant d of n, n

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