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();//如果注释掉这行代码,则此程序不会抛出异常
        }
    }
}
AI 代码解读

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);
    }
AI 代码解读

所以,当线程被标记有中断标志时,offer添加时将获取不到锁,直接抛出中断异常。

目录
打赏
0
0
0
0
2
分享
相关文章
|
7月前
|
线程池中线程抛了异常,该如何处理?
【8月更文挑战第27天】在Java多线程编程中,线程池(ThreadPool)是一种常用的并发处理工具,它能够有效地管理线程的生命周期,提高资源利用率,并简化并发编程的复杂性。然而,当线程池中的线程在执行任务时抛出异常,如果不妥善处理,这些异常可能会导致程序出现未预料的行为,甚至崩溃。因此,了解并掌握线程池异常处理机制至关重要。
618 0
阻塞队列中的安全中断
阻塞队列中的安全中断
201 0
阻塞队列中的安全中断
线程池内运行的线程抛异常,线程池会怎么办
线程池内运行的线程抛异常,线程池会怎么办
137 0
AQS源码探究_06 Conditon条件队列(await方法、线程入队与挂起、signal方法)
AQS源码探究_06 Conditon条件队列(await方法、线程入队与挂起、signal方法)
AQS源码探究_06 Conditon条件队列(await方法、线程入队与挂起、signal方法)
堵塞队列BlockingQueue 使用与理解
堵塞队列本质就是队列,底层数据结构 通常是由数组,或者链表构成。实现FIFO思想 当阻塞队列是空时,从队列中获取元素的操作将会被阻塞。 当阻塞队列是满时,往队列里添加元素的操作将会被阻塞。
201 0
线程与异常
最近遇到了一个c++线程抛出异常的问题 代码片段 #include &lt;iostream&gt; #include &lt;signal.h&gt; #include &lt;stdlib.h&gt; #include &lt;string&gt; #include &lt;thread&gt; #include &lt;unistd.h&gt; #include &
2621 0
并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出)。Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是BlockingQueue,非阻塞队列的典型例子是ConcurrentLinkedQueue,在实际应用中要根据实际需要选用阻塞队列或者非阻塞队列。
1098 0
并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法(转)
在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出)。Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是BlockingQueue,非阻塞队列的典型例子是ConcurrentLinkedQueue,在实际应用中要根据实际需要选用阻塞队列或者非阻塞队列。
1236 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等