LinkedBlockingQueue阻塞队列offer()操作抛出中断异常

简介:

说明

在使用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添加时将获取不到锁,直接抛出中断异常。

目录
相关文章
|
安全 Java 调度
线程的状态 and 线程安全
线程的状态 and 线程安全
53 0
|
6月前
|
监控 Java
ThreadPoolExecutor 线程执行超时,释放线程
ThreadPoolExecutor 线程执行超时,释放线程
197 1
|
6月前
|
Java
线程池中线程抛了异常,该如何处理?
【8月更文挑战第27天】在Java多线程编程中,线程池(ThreadPool)是一种常用的并发处理工具,它能够有效地管理线程的生命周期,提高资源利用率,并简化并发编程的复杂性。然而,当线程池中的线程在执行任务时抛出异常,如果不妥善处理,这些异常可能会导致程序出现未预料的行为,甚至崩溃。因此,了解并掌握线程池异常处理机制至关重要。
592 0
|
Java
线程池中阻塞队列的作用
线程池中阻塞队列的作用
167 0
|
9月前
|
缓存 安全 Java
多线程Thread(初阶三:线程的状态及线程安全)
多线程Thread(初阶三:线程的状态及线程安全)
88 0
|
Java
面试官:说一下线程池的状态以及线程池中空闲的线程的状态
面试官:说一下线程池的状态以及线程池中空闲的线程的状态
1276 0
|
Java
线程池内运行的线程抛异常,线程池会怎么办
线程池内运行的线程抛异常,线程池会怎么办
130 0
|
算法 Java 数据库
面试官:线程池里面到底该设置多少个线程?
关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下:
使用ThreadPoolExecutor,当提交线程超过maximumPoolSize 会阻塞主线程吗?
使用ThreadPoolExecutor,当提交线程超过maximumPoolSize 会阻塞主线程吗?
201 0
使用ThreadPoolExecutor,当提交线程超过maximumPoolSize 会阻塞主线程吗?