JDK8系列:刨根问底 之 队列

1、Queue 接口

public interface Queue<E> extends Collection<E> {

    // 将元素添加到这个队列中,但是如果没有可用空间,则抛出异常IllegalStateException
    boolean add(E e);

    // 入队
    boolean offer(E e);

    // 移除头部或者如果队列为空。则返回null
    E remove();

    // 出队
    E poll();

    // 如果为空,只抛出异常,检索头部
    E element();

    // 检索头部但不删除
    E peek();
}

 

2、AbstractQueue 抽象类

public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E> {

    // 构造方法
    protected AbstractQueue() {
    }

    // 添加元素e,实际上是调用的 offer(e)
    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }

    // 删除,实际上是调用的poll()
    public E remove() {
        E x = poll();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

    // 返回队头元素,实际上是调用的 peek()
    public E element() {
        E x = peek();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }

    // 清空
    public void clear() {
        while (poll() != null)
            ;
    }

    // 添加集合c中的所有元素,就是一个一个用add(e)加进来
    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;
    }

}

add(e)、remove()、element() 分别调用的 offer(e)、poll()、peek()。只不过是在其基础上增加了异常的处理。

 

3、BlockingQueue 接口

在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。

public interface BlockingQueue<E> extends Queue<E> {

    // 将元素e插入到队列末尾,如果插入成功,则返回true;如果插入失败(即队列已满),则会抛出异常
    boolean add(E e);   
    // 将元素e插入到队列末尾,如果插入成功,则返回true;如果插入失败(即队列已满),则返回false
    boolean offer(E e);
    // 阻塞方法:向队尾存入元素,如果队列满,则等待
    void put(E e) throws InterruptedException;
    // 向队尾存入元素,如果队列满,则等待一定的时间,当时间期限达到时,如果还没有插入成功,则返回false;否则返回true
    boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;

    // // 阻塞方法:取出队首元素,如果队列空,则等待
    E take() throws InterruptedException;
    // 从队首取元素,如果队列空,则等待一定的时间,当时间期限达到时,如果取不到,则返回null;否则返回取得的元素
    E poll(long timeout, TimeUnit unit) throws InterruptedException;
    // 移除队首元素,若移除成功,则返回true;如果移除失败(队列为空),则会抛出异常;
    boolean remove(Object o);

    // 获取队列中剩余的空间
    int remainingCapacity();

    // 是否包含o
    public boolean contains(Object o);

    // 从该队列中移除所有可用元素,并将它们添加到给定集合中
    int drainTo(Collection<? super E> c);
    // 指定最多数量限制将队列中值,全部移除,并发设置到给定的集合中
    int drainTo(Collection<? super E> c, int maxElements);
}

 

下面介绍7种阻塞队列:

JDK8系列:阻塞队列 之 ArrayBlockingQueue(有界阻塞队列)源码解析

JDK8系列:阻塞队列 之 LinkedBlockingQueue(有界阻塞队列)源码解析

JDK8系列:阻塞队列 之 PriorityBlockingQueue(优先级队列)源码分析

JDK8系列:阻塞队列 之 DelayQueue(延迟队列)源码分析

JDK8系列:阻塞队列 之 SynchronousQueue(同步队列)源码分析

JDK8系列:阻塞队列 之 LinkedTransferQueue(无界阻塞队列)源码解析

JDK8系列:阻塞队列 之 LinkedBlockingDeque(双向阻塞队列)源码解析

同时可以结合书籍:Java并发编程的艺术6.3小结看

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值