Queue: basically, a queue is a first-in-first-out (FIFO) data structure.
Offer, add differences:
Some queues have size restrictions. Therefore, if you want to add a new item to a full queue, the extra items will be rejected.
The new offer method can work at this time. It does not throw an unchecked exception for calling the add () method, but returns false returned by offer.
Poll, remove differences:
Both the remove () and Poll () Methods Delete the first element (head) from the queue ). The remove () action is similar to the Collection interface version,
However, the new poll () method does not throw an exception when calling an empty set, but returns NULL. Therefore, the new method is more suitable for situations that are prone to abnormal conditions.
Difference between peek and element:
Element () and peek () are used to query elements in the queue's header. Similar to the remove () method, element () throws an exception when the queue is empty, while PEEK () returns NULL.
There are two groups of queue implementations in Tiger: Implementing and not implementing the new blockingqueue Interface
1) List of unimplemented blocking interfaces: Java. util. Queue interface and Java. util. abstractqueue interface are implemented.
Built-in non-blocking queue: priorityqueue and concurrent1_queue
The priorityqueue and concurrentincluqueue classes add two specific set implementations to the Collection framework.
The priorityqueue class essentially maintains an ordered list. Elements added to the queue are sorted based on their natural order (implemented by Java. util. Comparable) or implemented by Java. util. comparator passed to the constructor.
Concurrentincluqueue is a thread-safe Queue Based on Link nodes. Concurrent access does not need to be synchronized. Because it adds elements to the end of the queue and deletes them from the header, as long as you do not need to know the size of the queue, concurrentincluqueue can work well for shared access to public sets. It is slow to collect information about the queue size. You need to traverse the queue.
2) Implement the blocking interface:
The new java. util. Concurrent package adds the blockingqueue interface and five blocking queue classes to the specific collection classes available in the Collection framework. It is essentially a one-bit distorted FIFO data structure. The thread does not immediately add or delete elements from the queue, and the operations are blocked until space or elements are available.
The five queues provide different functions:
* Arrayblockingqueue: A Bounded queue supported by arrays.
* Define blockingqueue: an optional bounded queue supported by a linked node.
* Priorityblockingqueue: an unbounded priority queue supported by the priority heap.
* Delayqueue: a time-based scheduling queue supported by the priority heap.
* Synchronousqueue: A simple aggregation (rendezvous) mechanism using the blockingqueue interface.
The first two types of arrayblockingqueue and linkedblockingqueue are almost the same, but they are different in the backup storage. linkedblockingqueue does not always have a capacity limit. The linkedblockingqueue class without a size limit never blocks queue waits when adding elements (at least not until there is an integer. max_value element ).
Priorityblockingqueue is a queue with unlimited capacity. It uses the comparable sorting order of the contained elements to maintain the elements in a logical order. It can be seen as a possible substitute for treeset. However, there is a skill for priorityblockingqueue. The iterator instance returned from iterator () does not need to return elements in priority order. If all elements must be traversed in the order of priority, use the toarray () method and sort them by themselves, such as arrays. Sort (PQ. toarray ()).
The new delayqueue implementation may be one of the most interesting (and complex) implementations. Elements added to the queue must implement the new delayed interface (only one method -- long getdelay (Java. util. Concurrent. timeunit unit )). Because there is no limit on the queue size, the addition can return immediately, but the elements cannot be retrieved from the queue before the delay time expires. If multiple elements are delayed, the first element with the longest expiration time is removed. In fact, it does not sound so complicated.
Synchronousqueue class is the simplest. It has no internal capacity. It is like a hand-delivery mechanism between threads. The producer that adds an element to the queue will wait for the consumer of another thread. When this consumer appears, this element is directly transmitted between the consumer and the producer and will never be added to the blocking queue.