BlockingQueue

BlockingQueue

BlockingQueue接口继承自Queue接口,在Queue的基础上增加了等待操作,简单来说就是获取元素时队列如果是空的,等待,插入元素时,队列已经满了,等待。

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

    boolean add(E e);

    boolean offer(E e);

    void put(E e) throws InterruptedException;

    boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException;

    E take() throws InterruptedException;

    E poll(long timeout, TimeUnit unit) throws InterruptedException;

    int remainingCapacity();

    boolean remove(Object o);

    public boolean contains(Object o);

    int drainTo(Collection<? super E> c);  //从这个队列中删除所有可用的元素,并将它们添加到给定的集合中。 

    int drainTo(Collection<? super E> c, int maxElements);  
}

这个接口定义的方法有几和Queue接口定义的一致,这里主要的区别在于,假设队列是空的,出队操作会怎么执行,包括抛异常,返回指定值和阻塞等待。看下面的表格很清晰。

抛异常指定值阻塞带超时的阻塞
插入add(e)offer(e)put(e)offer(e,time,unit)
移除remove()poll()take()poll(time,unit)
测试element()peak()

接下来看它的几个实现类,说明一下,BlockingQueue和它的实现类都是在java.util.concurrent包下的,肯定要保证线程安全的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值