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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值