In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the Queue knowledge points of Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the Queue knowledge points of Java"?
Java provides Quere, which is quite easy to use, with enhancements in version 1.5.
Queue: basically, a queue is a FIFO data structure
Offer,add differences:
Some queues have size limits, so if you want to add a new item to a full queue, the extra items will be rejected.
At this point, the new offer method can work. Instead of throwing a unchecked exception to a call to the add () method, it just gets the false returned by offer ().
Poll,remove differences:
Both the remove () and poll () methods remove the first element (head) from the queue. The behavior of remove () is similar to that of the Collection interface
But instead of throwing an exception when called with an empty collection, the new poll () method simply returns null. Therefore, the new method is more suitable for situations prone to abnormal conditions.
Peek,element differences:
Element () and peek () are used to query elements at the head of the queue. Similar to the remove () method, element () throws an exception when the queue is empty, while peek () returns null.
-
There are two sets of Queue implementations in Tiger: implement the new BlockingQueue interface
And unrealized.
Unimplemented blocking interface: LinkedList: implements the java.util.Queue interface
Java.util.AbstractQueue
Built-in non-blocking queues: PriorityQueue and ConcurrentLinkedQueue
The PriorityQueue and ConcurrentLinkedQueue classes add two concrete collection implementations to Collection Framework.
The PriorityQueue class essentially maintains an ordered list. The elements added to the Queue are located according to their natural ordering (through their java.util.Comparable implementation) or according to the java.util.Comparator implementation passed to the constructor.
ConcurrentLinkedQueue is a thread-safe queue based on linked nodes. Concurrent access does not require synchronization. Because it adds elements at the end of the queue and removes them from the header, ConcurrentLinkedQueue's shared access to public collections works fine as long as you don't need to know the size of the queue. Collecting information about the size of the queue is slow and requires traversing the queue.
-
To implement blocking interfaces:
The new java.util.concurrent package adds the BlockingQueue interface and five blocking queue classes to the concrete collection classes available in Collection Framework.
It is essentially an FIFO data structure with a little distortion. Instead of immediately adding or removing elements from the queue, the thread performs an operation blocking until space or elements are available.
Each of the five queues offers different things:
* ArrayBlockingQueue: a bounded queue supported by an array.
* LinkedBlockingQueue: an optional bounded queue supported by linked nodes.
* PriorityBlockingQueue: an unbounded priority queue supported by priority heap.
* DelayQueue: a time-based scheduling queue supported by the priority heap.
* SynchronousQueue: a simple rendezvous mechanism that utilizes the BlockingQueue interface.
The first two classes, ArrayBlockingQueue and LinkedBlockingQueue, are almost the same, except in terms of backup storage, and LinkedBlockingQueue does not always have capacity limits. A LinkedBlockingQueue class with no size bounds will never have a blocking queue wait when adding an element (at least not until there is an Integer.MAX_VALUE element in it).
A PriorityBlockingQueue is a queue with unbounded capacity that uses the Comparable sort order of the contained elements to maintain elements in logical order. It can be regarded as a possible substitute for TreeSet. But there is a trick for PriorityBlockingQueue. The Iterator instance returned from iterator () does not need to return elements in priority order. If you have to iterate through all the elements in order of priority, let them all go through the toArray () method and sort them yourself, like Arrays.sort (pq.toArray ()).
The new DelayQueue implementation is probably the most interesting (and complex) of these. Elements added to the queue must implement the new Delayed interface (there is only one method-- long getDelay (java.util.concurrent.TimeUnit unit)). Because there is no limit to the size of the queue, the addition can be returned immediately, but the element cannot be extracted from the queue until the delay time has passed. If multiple elements complete the delay, the element with the earliest / longest expiration time will be the first to be taken out. Actually, it's not as complicated as it sounds.
The SynchronousQueue class is the simplest. It has no internal capacity. It is like a hand-to-hand mechanism between threads. The producer who adds one element to the queue waits for the consumer of another thread. When the consumer appears, the element is passed directly between the consumer and the producer and is never added to the blocking queue.
-
Experimental results:
The document says that the queue of BlockingQueue: instead of immediately adding or removing elements from the queue, threads block operations until space or elements are available.
After an experiment, using put and take is like this, and the thread is waiting
Using offer returns false immediately.
At this point, I believe you have a deeper understanding of "what are the Queue knowledge points of Java?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.