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

AbstractQueue简介

    博客分类:
  • JUC
阅读更多
Queue接口定义:http://donald-draper.iteye.com/blog/2363491
/*
 * 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;

/**
 * This class provides skeletal implementations of some {@link Queue}
 * operations. The implementations in this class are appropriate when
 * the base implementation does [i]not[/i] allow <tt>null</tt>
 * elements.  Methods {@link #add add}, {@link #remove remove}, and
 * {@link #element element} are based on {@link #offer offer}, {@link
 * #poll poll}, and {@link #peek peek}, respectively, but throw
 * exceptions instead of indicating failure via <tt>false</tt> or
 * <tt>null</tt> returns.
 AbstractQueue提供了Queue的操作实现的基础。AbstractQueue可以作为不允许
 元素为null时,Queue的简单实现。add,remove,element方法分别基于offer,
 poll,peek的实现,但是当队列为null时,抛出异常,而不是返回false或null。

 * <p>A <tt>Queue</tt> implementation that extends this class must
 * minimally define a method {@link Queue#offer} which does not permit
 * insertion of <tt>null</tt> elements, along with methods {@link
 * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and
 * {@link Collection#iterator}.  Typically, additional methods will be
 * overridden as well.  If these requirements cannot be met, consider
 * instead subclassing {@link AbstractCollection}.
 *
Queue的实现必须实现offer方法,并且允许插入null;peek,poll,size方法最好
重写,如果这些要求不能满足,可以用AbstractCollection代替。
 * <p>This class is a member of the
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 * Java Collections Framework</a>.
 *
 * @since 1.5
 * @author Doug Lea
 * @param <E> the type of elements held in this collection
 */
public abstract class AbstractQueue<E>
    extends AbstractCollection<E>
    implements Queue<E> {

    /**
     * Constructor for use by subclasses.
     */
    protected AbstractQueue() {
    }

    /**
     * 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.
     *
     * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
     * else throws an <tt>IllegalStateException</tt>.
     *
     * @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
     */
     添加一个元素,成功返回ture,否则抛出异常
    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }

    /**
     * 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.
     *
     * <p>This implementation returns the result of <tt>poll</tt>
     * unless the queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    消费队列头元素,有则返回元素,为null,则抛出异常
    public E remove() {
        E x = poll();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

    /**
     * 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.
     *
     * <p>This implementation returns the result of <tt>peek</tt>
     * unless the queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    检查是否有元素,有则返回元素,无则抛出异常
    public E element() {
        E x = peek();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

    /**
     * Removes all of the elements from this queue.
     * The queue will be empty after this call returns.
     *
     * <p>This implementation repeatedly invokes {@link #poll poll} until it
     * returns <tt>null</tt>.
     */
     清空,循环poll,直到为空
    public void clear() {
        while (poll() != null)
            ;
    }

    /**
     * Adds all of the elements in the specified collection to this
     * queue.  Attempts to addAll of a queue to itself result in
     * <tt>IllegalArgumentException</tt>. Further, the behavior of
     * this operation is undefined if the specified collection is
     * modified while the operation is in progress.
     *
     * <p>This implementation iterates over the specified collection,
     * and adds each element returned by the iterator to this
     * queue, in turn.  A runtime exception encountered while
     * trying to add an element (including, in particular, a
     * <tt>null</tt> element) may result in only some of the elements
     * having been successfully added when the associated exception is
     * thrown.
     *
     * @param c collection containing elements to be added to this queue
     * @return <tt>true</tt> if this queue changed as a result of the call
     * @throws ClassCastException if the class of an element of the specified
     *         collection prevents it from being added to this queue
     * @throws NullPointerException if the specified collection contains a
     *         null element and this queue does not permit null elements,
     *         or if the specified collection is null
     * @throws IllegalArgumentException if some property of an element of the
     *         specified collection prevents it from being added to this
     *         queue, or if the specified collection is this queue
     * @throws IllegalStateException if not all the elements can be added at
     *         this time due to insertion restrictions
     * @see #add(Object)
     */
    //循环遍历集合元素,add到队列
    public boolean addAll(Collection<? extends E> c) {
        if (c == null)
            throw new NullPointerException();
        if (c == this)
            throw new IllegalArgumentException();
        boolean modified = false;
        for (E e : c)
            if (add(e))
                modified = true;
        return modified;
    }
}

总结:
AbstractQueue的add,remove,element方法分别基于offer,poll,peek的实现,但是当队列为null时,抛出异常,而不是返回false或null。offer,poll,peek,并没有实现待子类扩展。清空,循环poll,直到为空。addAll为循环遍历集合元素,add到队列;
附:AbstractCollection



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

相关推荐

    Java 多线程与并发(15-26)-JUC集合- ConcurrentLinkedQueue详解.pdf

    `ConcurrentLinkedQueue`继承自`AbstractQueue`,这是一个抽象类,定义了一些基本的队列操作方法。同时,它实现了`Queue`接口,该接口定义了一组标准的队列操作。此外,`ConcurrentLinkedQueue`还实现了`...

    【死磕Java集合】-集合源码分析.pdf

    ArrayBlockingQueue的继承体系中,它继承了AbstractQueue,实现了BlockingQueue接口。 ArrayBlockingQueue的主要属性包括元素数组itemArray、元素个数count和锁对象lock。ArrayBlockingQueue提供了多种构造方法,...

    并发容器之ArrayBlockingQueue和LinkedBlockingQueue实现原理详解

    它们都继承自AbstractQueue类,并实现了BlockingQueue接口,提供了线程安全的队列操作。 ArrayBlockingQueue实现原理: ArrayBlockingQueue内部使用数组存储元素,使用ReentrantLock来保证线程安全,使用Condition...

    IntegerQueue:队列实施文件示例

    1. 定义队列类:创建一个新的类,命名为IntegerQueue,继承自Java的AbstractQueue接口,这样可以自动获得部分队列操作的默认实现。 2. 内部数据结构:在IntegerQueue类中,我们需要一个数据结构来存储元素。可以...

    Java 基础核心总结 +经典算法大全.rar

    AbstractQueue 抽象类LinkedList ArrayDeque PriorityQueue 反射的思想及作用 反射的基本使用 获取类的 Class 对象构造类的实例化对象获取-个类的所有信息 获取类中的变量(Field) 获取类中的方法(Method) 获取类的...

    顺序队列实现源码(C、C++、Java)

    在Java中实现顺序队列,可以继承`AbstractQueue`或实现`Queue`接口,定义一个内部数组存储元素,并提供相应的添加、删除和查询方法。Java的自动内存管理使得开发者无需关注内存分配和释放,但需要注意并发环境下同步...

    java队列Java系列2021.pdf

    PriorityQueue是一个不阻塞的优先级队列,继承自AbstractQueue并实现了Serializable接口。它会根据元素的自然顺序或者根据构造函数提供的Comparator来排序元素。ConcurrentLinkedQueue是基于链接节点的线程安全队列...

    java中文帮助文档.pdf

    `AbstractList`、`AbstractListModel`、`AbstractMap`、`AbstractMethodError`、`AbstractPreferences`、`AbstractQueue`、`AbstractQueuedSynchronizer`、`AbstractSelectableChannel`、`AbstractSelector`、`...

    并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法

    `LinkedBlockingQueue` 同样是 `java.util.concurrent` 包下的一个线程安全的阻塞队列实现,它继承自 `AbstractQueue` 并实现了 `BlockingQueue` 接口。`LinkedBlockingQueue` 的特点是可以在队列满时阻塞生产者线程...

    JAVA:PriorityQueue

    - `java.util.AbstractQueue&lt;E&gt;` - `java.util.PriorityQueue&lt;E&gt;` #### 构造方法 - **PriorityQueue()**:使用默认的初始容量(通常是11)创建一个`PriorityQueue`,并根据元素的自然顺序进行排序。 - **...

    超全Java集合框架讲解.md

    - **`AbstractQueue`抽象类**:为`Queue`接口提供了一些基本实现。 - **`LinkedList`**(再次提到):除了可以用作列表外,还可以用作队列或栈。 - **`ArrayDeque`**:基于数组实现的双端队列,对于频繁的添加和删除...

    LMAXAbstractQueue

    《LMAX AbstractQueue:构建高性能交易系统的基石》 在金融交易领域,毫秒甚至微秒级别的性能差异都可能带来显著的利润变化。LMAX Disruptor,作为一个高性能、低延迟的消息处理框架,正是为了解决这类问题而设计的...

    stack.rar_嵌入式/单片机/硬件编程_C/C++_

    "abstractQueue.h"和"abstractStack.h"是两个头文件,它们可能定义了抽象的数据结构——队列和堆栈的接口。在C/C++编程中,使用抽象数据类型可以提供一种封装机制,隐藏内部实现细节,只对外提供必要的操作方法。这...

    2011_priority_queue_assignment

    - PriorityQueue是Java集合框架的一部分,继承自AbstractQueue接口,并实现了Serializable和Cloneable接口。 - 它的内部实现基于二叉堆,提供了插入、删除、检查最大元素(堆顶元素)以及查找特定元素的方法。 3....

    java.util.ArrayDeque类使用方法详解

    Java中的`java.util.ArrayDeque`类是一个高效且灵活的数据结构,它实现了`Deque`接口,同时也继承了`AbstractCollection`和`AbstractQueue`。`ArrayDeque`是一个基于可变数组的双端队列,它的主要特点是它使用一个...

    jtable增删改查和jtree连接

    import java.util.AbstractQueue; import java.util.Vector; import javax.swing.*; import javax.swing.table.*; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree....

Global site tag (gtag.js) - Google Analytics