首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LinkedBlockingQueue thowing

LinkedBlockingQueue thowing
EN

Stack Overflow用户
提问于 2011-04-12 21:26:15
回答 1查看 3K关注 0票数 0

我有这段代码。LinkedBlockingQueue只有在等待添加到队列时被中断时才会抛出Exception。但是这个队列是无界的,所以它应该尽快添加。为什么我的shutdown方法抛出一个InterruptedException

代码语言:javascript
复制
private final LinkedBlockingQueue<Message> messages= new LinkedBlockingQueue<Message>();

public void run(){
    LinkedList<Message> messages = new LinkedList<Message>(); 
    while (true){
        try{
            messages.clear();
            messages.add(this.messages.take());
                            this.messages.drainTo(messages);
            for (Message message:messages){
                if(message.isPoison())return;
                doSomething(message);
            }
        }catch(Exception e){
            getLogger().addException(e);
        }
    }
}


protected void add(Message m){
    try {
        messages.put(m);
    }catch (InterruptedException e) {
        getLogger().addException(e);
        addRollback(e);
    }
}

public void shutdown(){
    try{
        messages.put(MessageFactory.getPoison());
    }catch(InterruptedException e){
     //here an exception is thrown. Why?
    }

}
EN

回答 1

Stack Overflow用户

发布于 2011-04-12 21:29:16

如果线程处于中断状态,即Thread.interrupted() == true,则调用将抛出InterruptionException。这并不意味着线程在你使用put时被中断了,它可能在进入之前就已经处于这个状态了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5635923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档