BlockingQueue接口分析

本文介绍了阻塞队列的基本概念及使用方法,包括add、offer、put等插入操作,poll、take等移除操作,并通过生产者消费者模式展示了阻塞队列的实际应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

一个阻塞队列接口定义,当队列为空时,取数据等待;当队列满时,存数据等待。
BlockingQueue定义的常用方法如下:

\抛出异常特定值阻塞超时
Insertadd(e)offer(e)put(e)offer(e, time, unit)
Removeremove()poll()take()poll(time,unit)
Examieelement()peek()--

1)add(e):把e加到BlockingQueue里,即如果BlockingQueue可以容纳,则返回true,否则抛出异常

2)offer(e):表示如果可能的话,将e加到BlockingQueue里,即如果BlockingQueue可以容纳,则返回true,否则返回false.

3)put(e):把e加到BlockingQueue里,如果BlockQueue没有空间,则调用此方法的线程被阻断直到BlockingQueue里面有空间再继续.

4)poll(time,unit):取走BlockingQueue里排在首位的对象,若不能立即取出,则可以等time参数规定的时间,取不到时返回null

5)take():取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到Blocking有新的对象被加入为止

其中:BlockingQueue 不接受null 元素。试图add、put 或offer 一个null 元素时,某些实现会抛出NullPointerException。null 被用作指示poll 操作失败的警戒值。

阻塞队列不接受null值元素,尝试存入null值元素会抛异常。null值用来表示poll()操作失败。
阻塞队列常用场景:生产者消费者

 class Producer implements Runnable {
     private final BlockingQueue queue;
     Producer(BlockingQueue q) { queue = q; }
     public void run() {
       try {
         while (true) { queue.put(produce()); }
       } catch (InterruptedException ex) { ... handle ...}
     }
     Object produce() { ... }
   }

   class Consumer implements Runnable {
     private final BlockingQueue queue;
     Consumer(BlockingQueue q) { queue = q; }
     public void run() {
       try {
         while (true) { consume(queue.take()); }
       } catch (InterruptedException ex) { ... handle ...}
     }
     void consume(Object x) { ... }
   }

   class Setup {
     void main() {
       BlockingQueue q = new SomeQueueImplementation();
       Producer p = new Producer(q);
       Consumer c1 = new Consumer(q);
       Consumer c2 = new Consumer(q);
       new Thread(p).start();
       new Thread(c1).start();
       new Thread(c2).start();
     }
   }

某个线程向BlockingQueue中插入一个对象的操作happen-before随后的其他线程获取或删除操作这个对象的操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愤怒的可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值