Queue, a linear data structure supporting FIFO.
The most common scenario is to buy tickets in line, buy tickets first and leave first.
The new arrivals were at the end of the line. (of course, queue jumping is not allowed here, but there are many people in real life, alas.)
The queue interface is as follows:
//The queue element type is represented by the generic E, which is specified when using the implementation class, such as string, person, etc public interface Queue<E> extends Collection<E> //Basic interface methods include: /** * Add element without verification, return true successfully, throw exception in case of failure. * @throws IllegalStateException Insufficient space * @throws ClassCastException Incompatible type. The added element is not a queue element type or a subclass of the element. (that is, all elements in the queue must be of the same class) * @throws NullPointerException The added element is null * @throws IllegalArgumentException if some property of this element prevents it from being added to this queue(Literally, adding some attributes of an element prevents it from joining the queue) */ boolean add(E e); /** * There is one more capacity verification than add. If it is successfully added, true will be returned. If the capacity is not enough, false will be returned. */ boolean offer(E e); /** * Remove the element from the queue header and return * This method does not check whether the queue is empty. If the queue is empty, a NoSuchElementException exception will be thrown */ E remove(); /** * Remove the element from the queue header and return * Unlike remove, this method returns null if the queue is empty */ E poll(); /** * Returns an element from the queue header without deleting it * Queue is empty, throw NoSuchElementException exception */ E element(); /** * Returns an element from the queue header without deleting it * Queue is empty, return null */ E peek();
Add corresponds to offer, which is used to add elements. If the former is not verified, throw an exception if there is not enough space, and if there is not enough space, return false.
remove corresponds to poll, which is used to delete and get the team head element. The former does not perform validation, and throws exceptions if there is no element, and the latter returns null.
Element corresponds to peek, which is used to get the team head element. The former does not perform validation, and throws an exception if there is no element, and the latter returns null.
-The queue is empty, and the thread that gets the element waits for the queue to become non empty
-The queue is full, the thread that gets the element waits for the queue to be available
Blocking queue interface
public interface BlockingQueue<E> extends Queue<E> //Based on the Queue, the following methods are extended: /** * Add element to queue * When the queue is full, it will block and wait for the queue to be available * Waiting to be interrupted will throw InterruptedException */ void put(E e) throws InterruptedException; /** * Remove and return elements from the queue header * If the queue is empty, it will block and wait for elements in the queue * Waiting to be interrupted will throw InterruptedException */ E take() throws InterruptedException; /** * If the queue is empty, it will block and wait for elements in the queue * If the queue is empty, it will block. There are elements in the waiting queue. The waiting time is specified by timeout. Unit is the unit of waiting time * Timeout failed to get element, return null */ E poll(long timeout, TimeUnit unit) throws InterruptedException; /** * Check how many more elements can be inserted into the queue (ideally downward, because after checking, other threads may have already operated the queue) * If it is an unbounded queue, integer.max? Value is returned. */ int remainingCapacity(); /** * Delete the specified element from the queue, if there are more than one, only one */ boolean remove(Object o); /** * Determine whether an element is contained in the queue */ boolean contains(Object o); /** * Move elements, move all elements in the queue to the given collection (elements in the original queue will be deleted) * Returns the number of elements transferred */ int drainTo(Collection<? super E> c); /** * Element moving, with an additional one specifying how many elements can be moved to the new collection at most */ int drainTo(Collection<? super E> c, int maxElements);
- Compared with ordinary queues, there are more blocking put and offer methods to add elements. The overload method of offer provides a blocking time.
- Compared with ordinary queues, there are more blocking take and poll methods to get elements. Poll's overload method provides a blocking time.
- Add just add, not enough space to throw exceptions.
- offer can only be added if there is enough space. If there is not enough space, return false.
- put space enough to add, not enough to wait.
- If the space of offer (overload) is enough, it can be added. If it is not enough to wait for the specified time, the timeout returns null.
- remove just delete, no element throwing exception
- poll no element returns null
- take no elements, just wait
- poll (overload) has no element waiting for the specified time, and the timeout returns null.