`
Donald_Draper
  • 浏览: 950619 次
社区版块
存档分类
最新评论

Queue接口定义

    博客分类:
  • JUC
阅读更多
/*
 * 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


  • 大小: 15.1 KB
0
0
分享到:
评论

相关推荐

    一文弄懂java中的Queue家族

    文章目录简介Queue接口Queue的分类BlockingQueueDequeTransferQueue总结 java中Queue家族简介 简介 java中Collection集合有三大家族List,Set和Queue。当然Map也算是一种集合类,但Map并不继承Collection接口。 List...

    stm32-freeRTOS-queue

    主要介绍Queue的相关知识,包括Queue的定义,发送和接收消息的方式等内容。重点使用Free RTOS中Queue的接口,实现数据在不同task之间的发送和接收的案例,并在板卡上验证该功能。

    华科java实验-用泛型栈实现泛型队列

    (完整项目,可直接食用)Java提供的java.util.Queue是一个接口没有构造函数。试用java.util.Stack泛型栈作为父类,用另一...除提供无参构造函数Queue( )外,其它所有队列函数严格参照java.util.Queue的接口定义实现。

    40道java集合面试题含答案(很全很详细)

    Collection接口是List、Set和Queue接口的父接口,定义了存取一组对象的方法。Collection接口继承自Iterable接口,这意味着可以通过for-each循环遍历集合元素。 List接口是有序的集合,可以包含重复的元素。List接口...

    queue-it:一个简单的排队 API

    为了将此应用程序安装到您本地的 ArangoDB,您可以像这样使用 Foxx-manager foxx-manager install queue-it /queue这会将应用程序下载到 ArangoDB 的本地安装(必须已启动并运行)并将其安装在路径“/queue”下。...

    JobQueue:PHP JobQueue组件可用于排队将由分布式工作人员提取的任务,这些工作应执行可自定义的工作

    PHP JobQueue组件 简单的Job / Queue PHP组件,可帮助应用程序通过多个工作程序分发任务。安装该软件包可通过Composer安装并自动加载: $ ... 必须在实现ExecutableJob接口PHP类中定义每个作业: &lt;?phpuse JobQ

    java 停车厂模拟管理程序的设计与实现

    [问题描述] 设停车厂只有一个可停放几辆汽车的狭长通道,且只有—个大门可供汽车进出。汽车在停车场内按车辆到达的先后顺序依次排列,若...3.定义Queue接口 4.设计等待队列WaitQueue 5.定义结点类Node 6.搭建管理界面

    Java编程实践:10个实用例子助您提升技能正则表达式、文件操作、日期和时间处理、数据结构、集合类、接口和多态、递归、多线程编程

    9. 使用Java集合类实现队列:演示了如何使用Java集合类中的Queue接口来实现队列数据结构。 10. 多线程编程:创建和启动线程:展示了如何通过实现Runnable接口创建一个新线程,并演示了多线程编程的基本概念。

    详细分析android的MessageQueue.IdleHandler

    一般我们比较少接触MessageQueue, 其实它内部的IdleHandler接口有很多有趣的用法,首先看看它的定义: 简而言之,就是在looper里面的message暂时处理完了,这个时候会回调这个接口,返回false,那么就会移除它,...

    IOCP_API(2.4)

    定义IRPC接口用来处理同步调用的被调用端 定义Bind函数用来绑定IRPC接口的实例 定义CallEx函数用来实现用户同步调用 2.3 添加bInit变量,用于设置初始化和关闭状态。 设置此变量的目的主要是用来控制,在关闭了...

    config_loader:Message Queue配置加载程序类型结构

    msqueue_config_loader 定义消息队列配置结构并实现LoadByFile和LoadByBytes接口。 type QueueConfig struct {Url string `json:"url" yaml:"url"`AccessKeyId string `json:"access_key_id" yaml:"access_key_id"`...

    优先队列(priority_queue)的C语言实现代码

    优先队列(priority_queue)和一般队列(queue)的函数接口一致,不同的是,优先队列每次出列的是整个队列中最小(或者最大)的元素。 本文简要介绍一种基于数组二叉堆实现的优先队列,定义的数据结构和实现的函数接口...

    标准模板库STL

    简单地说就是一种接口类,专门用来修改现有类的接口,提供一种新的接口;或调用现有的函数来实现所需要的功能。 主要包括3种适配器Container Adaptors、Iterator Adaptors与Function Adaptors。其中迭代器适配器...

    Apple推送服务(APNs)Java客户端zpush.zip

    Notification发送者可以自己定义设置发送的Queue,自己灵活处理阻塞,超时等问题。   Example: 更多的例子在src/test/java 目录下。 public class MainExample {  public static void main...

    C#5.0本质论第四版(因文件较大传的是百度网盘地址)

    16.2.7 队列集合:Queue 455 16.2.8 链表:LinkedList 456 16.3 提供索引器 456 16.4 返回null或者空集合 459 16.5 迭代器 459 16.5.1 迭代器的定义 460 16.5.2 迭代器语法 460 16.5.3 ...

    .NET 分布式组件库 Exceptionless Foundatio.zip

    Foundatio提供了一下不同的方法来定义一个Job。1、Jobs:提供了一、IJob 接口,和一个默认的基类JobBase。直接上代码吧:using Foundatio.Jobs; public class HelloWorldJob : JobBase {  public int ...

    Visual C#2010 从入门到精通(Visual.C#.2010.Step.By.Step).完整去密码锁定版 I部分

    第13章 创建接口和定义抽象类 227 13.1 理解接口 227 13.1.1 定义接口 228 13.1.2 实现接口 228 13.1.3 通过接口来引用一个类 230 13.1.4 使用多个接口 230 13.1.5 显式实现接口 230 13.1.6 接口的限制 232 ...

    TCP/IP症状和原因

     线路速率大于接口能力 1) 使用hold-queue减少进入的队列尺寸2) 增加输出的队列尺寸  接口速率大于线路速率 1) 减少广播流量2) 增加输出的队列3) 如有需要,使用队列算法 二、处理帧中继故障  DLCI用于在...

    PPP:PPP使用LCP设置和维护链路

     线路速率大于接口能力 1) 使用hold-queue减少进入的队列尺寸2) 增加输出的队列尺寸  接口速率大于线路速率 1) 减少广播流量2) 增加输出的队列3) 如有需要,使用队列算法 二、处理帧中继故障  DLCI用于在...

Global site tag (gtag.js) - Google Analytics