LinkedBlockingQueue

本文介绍了Java并发包中LinkedBlockingQueue的实现原理与使用方法。它实现了阻塞队列接口,内部采用链表结构存储元素,并可选地设置容量上限,默认为Integer.MAX_VALUE。元素按照先进先出(FIFO)原则存储,提供了put和take方法来实现线程间的同步。

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

The LinkedBlockingQueue class implements the BlockingQueue interface. Read the BlockingQueue text for more information about the interface.

The LinkedBlockingQueue keeps the elements internally in a linked structure (linked nodes). This linked structure can optionally have an upper bound if desired. If no upper bound is specified, Integer.MAX_VALUE is used as the upper bound.

The LinkedBlockingQueue stores the elements internally in FIFO (First In, First Out) order. The head of the queue is the element which has been in queue the longest time, and the tail of the queue is the element which has been in the queue the shortest time.

Here is how to instantiate and use a LinkedBlockingQueue:

BlockingQueue<String> unbounded = new LinkedBlockingQueue<String>();
BlockingQueue<String> bounded   = new LinkedBlockingQueue<String>(1024);

bounded.put("Value");

String value = bounded.take();
### Java `LinkedBlockingQueue` 使用方法 #### 创建 `LinkedBlockingQueue` 可以通过无参构造函数创建一个具有默认容量(`Integer.MAX_VALUE`)的队列,也可以通过指定容量的方式创建: ```java // 默认容量为 Integer.MAX_VALUE 的 LinkedBlockingQueue LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>(); // 容量为 10 的 LinkedBlockingQueue LinkedBlockingQueue<Integer> boundedQueue = new LinkedBlockingQueue<>(10); ``` [^2] #### 基本操作 - **插入元素** 向队列中添加元素的方法有多种选择。对于不会抛出异常的操作,当尝试往已满的队列中加入新项时会返回特定的结果;而对于可能会抛出异常的情况,则会在违反约束条件时立即报错。 ```java boolean offerResult = queue.offer("item"); // 如果成功则返回 true, 否则 false (不等待) try { boolean timedOfferResult = queue.offer("another item", 5L, TimeUnit.SECONDS); // 尝试最多五秒的时间去放置项目 } catch (InterruptedException e) { Thread.currentThread().interrupt(); } ``` [^1] - **移除元素** 从队列头部取出并删除元素也有几种不同的方式可以选择,具体取决于是否希望在遇到空队列的情况下得到提示还是愿意无限期地等待直至可用为止。 ```java String pollItem = queue.poll(); // 获取头元素并移除此元素; 若为空则返回 null 或者超时期间未找到任何东西就返回null try { String takeItem = queue.take(); // 取得下一个待处理的任务,如果没有任务存在就会一直阻塞当前线程直到有新的条目到来 } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } // 移除所有元素 queue.clear(); ``` - **查询队列状态** 有时需要检查队列的状态而不做修改动作,比如查看是否有剩余空间或者确认某个对象是否存在其中。 ```java int size = queue.size(); // 返回队列中的元素数目 boolean containsElement = queue.contains("test element"); ``` [^4] #### 生产者-消费者模式示例 下面是一个简单的例子展示了如何利用 `LinkedBlockingQueue` 实现经典的生产者-消费者问题解决方案: ```java class Producer implements Runnable { private final BlockingQueue<String> queue; public Producer(BlockingQueue<String> q) { this.queue = q; } @Override public void run() { try { while(true){ String product = "product-" + System.currentTimeMillis(); queue.put(product); System.out.println(Thread.currentThread().getName()+" produced "+product); Thread.sleep((long)(Math.random()*100)); } }catch(Exception ex){ /* handle exception */ } } } class Consumer implements Runnable { private final BlockingQueue<String> queue; public Consumer(BlockingQueue<String> q) {this.queue=q;} @Override public void run(){ try{ while(true){ String consumedProduct=queue.take(); System.out.println(Thread.currentThread().getName()+" consumed "+consumedProduct); Thread.sleep((long)(Math.random()*100)); } }catch(Exception ex){/*handle exception*/} } } public class Main { public static void main(String[] args)throws Exception{ int capacity = 10; LinkedBlockingQueue<String> sharedQueue=new LinkedBlockingQueue<>(capacity); ExecutorService executor=Executors.newCachedThreadPool(); for(int i=0;i<3;++i)//启动三个生产者线程 executor.execute(new Producer(sharedQueue)); for(int j=0;j<2;++j)//启动两个消费者线程 executor.execute(new Consumer(sharedQueue)); Thread.sleep(5000);//让程序运行一段时间后结束 executor.shutdownNow();//关闭执行服务 } } ``` [^5]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值