.NET 6 introduces a new collection type, PriorityQueue, which, as its name suggests, adds priority support on top of ordinary Queue. Note: He isNon-thread-safe, you need to pay attention to thread safety issues.
Source:The hyperlink login is visible.
Get Started
Let's take a look at a simple example of use:
Example output:
You can see that the order of output is opposite to the order we added, PriorityQueue starts with the smallest priority when dequeue, the smaller the value, the higher the priority, the higher the priority, the higher the priority, the more priority the output, if we want the maximum output first, is it okay, the answer is yes, but we need to specify our own priority comparison rules, you can refer to the following example
Scenes
With auto-sorting with priority, we can consider using PriorityQueue when we need to do some auto-sorting
Message Queue
With PriorityQueue, a priority message queue can be implemented, allowing users to specify the priority of a message when sending a message, and it will be prioritized when consuming.
In the above example, we specify an int as the type of Priority by default, and merge some messages into the queue, but there are often many messages, and there may be situations with the same priority, which we can usetime and int as a federated priority typeYou can refer to the following example:
An example of the output is as follows:
From the above results, it can be seen that in the case of priority, we will first process messages with less time, or we can customize the sorting method according to our own needs, and customize the priority comparison logic.
Rank
PriorityQueue can also be used in many ranking applications, such as a ranking of student performance
Take a look at the example code below:
The above list is a list of grades, just write a few test data, through PriorityQueue's UnorderedItems we can get the data before sorting, and it is also the order in which we join the queue (Enqueue), the default comparison is the small first, that is, the low score is first, then we want to sort from largest to smallest we need to customize the comparison method.
The above High2LowComparer is a custom comparison, which is actually an inverse of the comparison result, and the code is as follows:
The output above is as follows:
More
There is a zset(sortedSet) type of data in Redis that can do similar things, but there are still some differences between zset and PriorityQueue, zset is a set, a collection that automatically deduplicates, while PriorityQueue is still a Queue will not be deduplicated, zset can modify the priority(score) of the corresponding element, but PriorityQueue Currently, it is not supported to modify the corresponding priority of an element
PriorityQueue can solve some of our problems, but there are a few things to keep in mind when using it:
- First of all, if the priority is the same, the order of the output may be different, which is determined by its internal implementation algorithm, and the order cannot be strictly guaranteed
- PriorityQueue is non-thread-safe, and thread-safe issues need to be noted
- The Peek method in PriorityQueue will only fetch the upcoming element in the queue, but will not remove it from the queue
(End) |