/* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ package java.util; /** * A collection designed for holding elements prior to processing. * Besides basic {@link java.util.Collection Collection} operations, * queues provide additional insertion, extraction, and inspection * operations. Each of these methods exists in two forms: one throws * an exception if the operation fails, the other returns a special * value (either <tt>null</tt> or <tt>false</tt>, depending on the * operation). The latter form of the insert operation is designed * specifically for use with capacity-restricted <tt>Queue</tt> * implementations; in most implementations, insert operations cannot * fail. * Queue优先处理元素,除了Collection的基本操作之外,Queue添加了另外插入,拉取, 检查3个操作。每一种方法都有两种格式,一种操作失败,则抛出异常,另一种返回一个 特殊的值,null或false,依赖于操作。后一种形式的插入操作是用capacity-restricted Queue来实现的,在大多数的实现中,插入操作不会失败。 * <p> * <table BORDER CELLPADDING=3 CELLSPACING=1> * <tr> * <td></td> * <td ALIGN=CENTER>[i]Throws exception[/i]</td> * <td ALIGN=CENTER>[i]Returns special value[/i]</td> * </tr> * <tr> 插入,提供 * <td><b>Insert</b></td> * <td>{@link #add add(e)}</td> * <td>{@link #offer offer(e)}</td> * </tr> * <tr>移除,拉取 * <td><b>Remove</b></td> * <td>{@link #remove remove()}</td> * <td>{@link #poll poll()}</td> * </tr> * <tr>检查 * <td><b>Examine</b></td> * <td>{@link #element element()}</td> * <td>{@link #peek peek()}</td> * </tr> * </table> * * <p>Queues typically, but do not necessarily, order elements in a * FIFO (first-in-first-out) manner. Among the exceptions are * priority queues, which order elements according to a supplied * comparator, or the elements' natural ordering, and LIFO queues (or * stacks) which order the elements LIFO (last-in-first-out). * Whatever the ordering used, the [i]head[/i] of the queue is that * element which would be removed by a call to {@link #remove() } or * {@link #poll()}. In a FIFO queue, all new elements are inserted at * the [i] tail[/i] of the queue. Other kinds of queues may use * different placement rules. Every <tt>Queue</tt> implementation * must specify its ordering properties. * Queues除了非必要之外,元素的模式为FIFO。其中的一个例外是优先级队列,元素根据 比较器或者本身的顺序添加队列( LIFO queues)中。无论什么顺序,对头的元素将会 被#remove或#poll消费/移除。在先进先出队列中,所有的新元素插入到队尾,其他队列 可能会用其他的策略添加新元素。每一种队列的实现要明确排序策略。 * <p>The {@link #offer offer} method inserts an element if possible, * otherwise returning <tt>false</tt>. This differs from the {@link * java.util.Collection#add Collection.add} method, which can fail to * add an element only by throwing an unchecked exception. The * <tt>offer</tt> method is designed for use when failure is a normal, * rather than exceptional occurrence, for example, in fixed-capacity * (or "bounded") queues. * 如果可能,offer操作,插入元素时,会返回false。这个与集合类的add方法不同的时, 集合类add方法,当失败时,则抛出一个unchecked异常。offer操作设计为在一个正常模式下的失败, 而不异常发生,比如队列容量有限。 * <p>The {@link #remove()} and {@link #poll()} methods remove and * return the head of the queue. * Exactly which element is removed from the queue is a * function of the queue's ordering policy, which differs from * implementation to implementation. The <tt>remove()</tt> and * <tt>poll()</tt> methods differ only in their behavior when the * queue is empty: the <tt>remove()</tt> method throws an exception, * while the <tt>poll()</tt> method returns <tt>null</tt>. * * <p>The {@link #element()} and {@link #peek()} methods return, but do * not remove, the head of the queue. * remove和poll元素将从队列头部消费一个元素。不同的队列实现消费队列元素顺序时,可能有不同的 策略。remove和poll操作不同的时,当队列为空时,remove抛出异常 ,poll则返回null。 * <p>The <tt>Queue</tt> interface does not define the <i>blocking queue * methods</i>, which are common in concurrent programming. These methods, * which wait for elements to appear or for space to become available, are * defined in the {@link java.util.concurrent.BlockingQueue} interface, which * extends this interface. * Queue没有定义阻塞队列的方法,这些方法会在并发编程中用到。这些方法,比如,在队列为空时, 消费者阻塞等待队列有元素可利用,这些方法在BlockingQueue接口中定义。 * <p><tt>Queue</tt> implementations generally do not allow insertion * of <tt>null</tt> elements, although some implementations, such as * {@link LinkedList}, do not prohibit insertion of <tt>null</tt>. * Even in the implementations that permit it, <tt>null</tt> should * not be inserted into a <tt>Queue</tt>, as <tt>null</tt> is also * used as a special return value by the <tt>poll</tt> method to * indicate that the queue contains no elements. 队列的实现通常不允许插入null,尽管LinkedList不禁止插入null值。即使允许插入null, 我们也利用利用poll操作,来暗示队列中没有元素。 * * <p><tt>Queue</tt> implementations generally do not define * element-based versions of methods <tt>equals</tt> and * <tt>hashCode</tt> but instead inherit the identity based versions * from class <tt>Object</tt>, because element-based equality is not * always well-defined for queues with the same elements but different * ordering properties. * Queue的实现通常不会定义equals和hashCode方法,而是继承自Object,因为hash值和值相等 ,但是顺序属性可能不同。 * * <p>This interface is a member of the * <a href="{@docRoot}/../technotes/guides/collections/index.html"> * Java Collections Framework</a>. * * @see java.util.Collection * @see LinkedList * @see PriorityQueue * @see java.util.concurrent.LinkedBlockingQueue * @see java.util.concurrent.BlockingQueue * @see java.util.concurrent.ArrayBlockingQueue * @see java.util.concurrent.LinkedBlockingQueue * @see java.util.concurrent.PriorityBlockingQueue * @since 1.5 * @author Doug Lea * @param <E> the type of elements held in this collection */ public interface Queue<E> extends Collection<E> { /** * Inserts the specified element into this queue if it is possible to do so * immediately without violating capacity restrictions, returning * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt> * if no space is currently available. * 在有界队列未满的情况下,添加元素,返回true,没空间可利用,则抛出异常IllegalStateException * @param e the element to add * @return <tt>true</tt> (as specified by {@link Collection#add}) * @throws IllegalStateException if the element cannot be added at this * time due to capacity restrictions * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null and * this queue does not permit null elements * @throws IllegalArgumentException if some property of this element * prevents it from being added to this queue */ boolean add(E e); /** * Inserts the specified element into this queue if it is possible to do * so immediately without violating capacity restrictions. * When using a capacity-restricted queue, this method is generally * preferable to {@link #add}, which can fail to insert an element only * by throwing an exception. * 在有界队列未满的情况下,添加元素,立即成功;当队列为有界是,add更好,因为越界抛出异常 * @param e the element to add * @return <tt>true</tt> if the element was added to this queue, else * <tt>false</tt> * @throws ClassCastException if the class of the specified element * prevents it from being added to this queue * @throws NullPointerException if the specified element is null and * this queue does not permit null elements * @throws IllegalArgumentException if some property of this element * prevents it from being added to this queue */ boolean offer(E e); /** * Retrieves and removes the head of this queue. This method differs * from {@link #poll poll} only in that it throws an exception if this * queue is empty. * 从对头消费一个元素,当队列为空,抛出异常 * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ E remove(); /** * Retrieves and removes the head of this queue, * or returns <tt>null</tt> if this queue is empty. * 从对头消费一个元素,当队列为空,返回null * @return the head of this queue, or <tt>null</tt> if this queue is empty */ E poll(); /** * Retrieves, but does not remove, the head of this queue. This method * differs from {@link #peek peek} only in that it throws an exception * if this queue is empty. * 检查队列中是否有元素,当队列为空,抛出异常 * @return the head of this queue * @throws NoSuchElementException if this queue is empty */ E element(); /** * Retrieves, but does not remove, the head of this queue, * or returns <tt>null</tt> if this queue is empty. * 检查队列中是否有元素,当队列为空,返回null * @return the head of this queue, or <tt>null</tt> if this queue is empty */ E peek(); }
附:Collection