LinkedBlockingQueue阻塞队列offer()操作抛出中断异常
来源:互联网 发布:上海通用在线编程网址 编辑:程序博客网 时间:2024/06/08 02:08
说明
在使用LinkedBlockingQueue的offer方法时,出现了中断异常,现分析一下出现这个中断异常的原因。
会产生中断异常的Demo
import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.TimeUnit;public class TestLinkedBlockingQueue { public static void main(String[] args) { LinkedBlockingQueue lbq = new LinkedBlockingQueue(3); for(int i = 0; i < 10; i++){ try { System.out.println(lbq); //这里用offer方法往阻塞队列里面添加对象,此方法表示若队列满了,则等待1秒,1秒后若队列还是满的,则丢弃数据。 lbq.offer(i, 1000, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Thread.currentThread().interrupt();//如果注释掉这行代码,则此程序不会抛出异常 } }}
LinkedBlockingQueue的offer源码方法
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException { if (e == null) throw new NullPointerException(); long nanos = unit.toNanos(timeout); int c = -1; final ReentrantLock putLock = this.putLock; final AtomicInteger count = this.count; putLock.lockInterruptibly(); try { while (count.get() == capacity) { if (nanos <= 0) return false; nanos = notFull.awaitNanos(nanos); } enqueue(new Node<E>(e)); c = count.getAndIncrement(); if (c + 1 < capacity) notFull.signal(); } finally { putLock.unlock(); } if (c == 0) signalNotEmpty(); return true; } //lockInterruptibly方法 public void lockInterruptibly() throws InterruptedException { sync.acquireInterruptibly(1); } //acquireInterruptibly方法 public final void acquireInterruptibly(int arg) throws InterruptedException { if (Thread.interrupted()) throw new InterruptedException(); if (!tryAcquire(arg)) doAcquireInterruptibly(arg); }
所以,当线程被标记有中断标志时,offer添加时将获取不到锁,直接抛出中断异常。
阅读全文
0 0
- LinkedBlockingQueue阻塞队列offer()操作抛出中断异常
- 阻塞队列 BlockingQueue LinkedBlockingQueue
- 阻塞队列--LinkedBlockingQueue
- 阻塞队列LinkedBlockingQueue用法
- 阻塞队列--LinkedBlockingQueue
- 链阻塞队列 LinkedBlockingQueue
- 阻塞队列之LinkedBlockingQueue
- mysql 触发器 trigger 中断操作 抛出异常
- 四 : LinkedBlockingQueue 链阻塞队列
- linkedBlockingQueue实现事件阻塞队列
- 阻塞队列LinkedBlockingQueue源码分析
- 阻塞队列LinkedBlockingQueue源码分析
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue在入队操作高并发性能比较
- 基于LinkedBlockingQueue源码自我实现阻塞队列
- 阻塞队列--线程安全的LinkedBlockingQueue
- httpclient 传递参数的问题
- canvas实现雨滴效果
- 交换两变量的值
- 人脸检测之MTCNN训练自己的数据
- 一个简易的allocator
- LinkedBlockingQueue阻塞队列offer()操作抛出中断异常
- Python 爬虫——requests库
- 超级账本PBFT(拜占庭容错)算法详解
- eclipse+pydev环境搭建(jdk环境配置相关)
- python学习之异常与多线程
- C 语言位操作
- 语料与词汇资源
- android的web浏览器配置属性
- Binder机制研究