为什么Thread.isInterrupted()总是返回false? [英] Why does Thread.isInterrupted () always return false?

查看:456
本文介绍了为什么Thread.isInterrupted()总是返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了JavaDoc的方法:

I found the method of JavaDoc:

返回:
如果此线程已被中断,否则为假。

我认为我对方法的理解有问题。此外,我可能误解Thread中的概念中断。

I think something wrong with my understanding of the method. Further, I may misunderstand the concept ‘interrupt’ in Thread.

欢迎任何解释!

代码段:

在线程定义中:

public void run() {
        try {
            //Do something
        } catch (InterruptedException e) {
            System.out.println(isInterrupted());//Always false
            return;
        }
    }

invoke:

theThread.interrupt();


推荐答案

抛出异常后,线程不再处于中断状态。 你可以使用这个方法,如果你想检查是否有东西请求线程被中断没有进入等待状态(因为InterruptedException是,据我所知,只有当线程处于等待状态时抛出)。(编辑 - 见我的意见:Stephen C)

Once the exception is thrown, the thread is no longer in an interrupted state. You might use this method if you want to check if something requested the thread to be interrupted without entering a wait state (since InterruptedException is, to my knowledge, only thrown when the thread is in a wait state). (Edited - see my comments: Stephen C)

这篇关于为什么Thread.isInterrupted()总是返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆