java - java中interrupt()方法与线程状态的精确行为

标签 java multithreading interrupt thread-sleep interrupted-exception

我已阅读下面的帖子

What does java.lang.Thread.interrupt() do?但我没能完全正确

我引用@Mike_q对上述问题的回答如下

Thread.interrupt() sets the interrupted status/flag of the target thread. Then code running in that target thread MAY poll the interrupted status and handle it appropriately. Some methods that block such as Object.wait() may consume the interrupted status immediately and throw an appropriate exception (usually InterruptedException)

它说当对象处于 WAITING 状态时它可以消耗中断状态,那么当它处于 BLOCKED 状态等待对象的锁时会发生什么......?

我在下面尝试了该场景,代码是

在 X:t2 被阻止

    public class interruptsyc
{
    static Object resource = new Object();
    public static void main(String []args)
    {
        System.out.println("started main");
        Thread1 t1=new Thread1("thread 1");
        t1.start();
        delay(1000);
        Thread2 t2 =new Thread2("thread 2");
        t2.start();
        delay(1000);
        t2.interrupt(); // X: at this point t2 is in blocked state waiting for resource's lock
        System.out.println(t2.getName()+t2.interrupted());
        delay(1000);
        System.out.println("end main");

    }
    static void delay(long n)
    {
        try
        {
        Thread.sleep(n);
        }
        catch(InterruptedException ex)
        {
            System.out.println(Thread.currentThread().getName()+Thread.interrupted());
            ex.printStackTrace();
        }
    }
    static class Thread1 extends Thread{

        Thread1(String name)
        {
            setName(name);
        }
        public void run()
        {
            synchronized(resource)
            {
                System.out.println("start 1");
                delay(6000);
                System.out.println("end 1");
            }
        }
    }
    static class Thread2 extends Thread{
        Thread2(String name )
        {
        setName(name);  
        }
        public void run()
        {
            synchronized(resource)
            {
                System.out.println("start 2");
                delay(2000);
                System.out.println("end 2");
            }
        }
    }
}

输出如下

started main
start 1
false
end main
end 1
start 2
thread 2false
java.lang.InterruptedException: sleep interrupted
        at java.lang.Thread.sleep(Native Method)
        at interruptsyc.delay(interruptsyc.java:25)
        at interruptsyc$Thread2.run(interruptsyc.java:59)
end 2

似乎已经调用了InterruptedException,当稍后调用sleep方法时...为什么会这样...?

再次,我不太明白这里所说的是什么是民意调查

Polling occurs via the Thread.interrupted() method which returns the current thread's interrupted status AND clears that interrupt flag. Usually the thread might then do something such as throw InterruptedException.

每当我在上面的代码中调用 Thread2.interrupted() 方法时,它都会返回 false(当我在 t2.interrupt 之后和 catch block 中调用时)

最佳答案

it seems that InterruptedException has been called ,when sleep method is called later... why is that ...?

因为阻塞/ sleep 方法不会立即阻塞。他们首先检查线程是否已被中断,如果是,则立即抛出 InterruptedException,以便线程尽快停止。

whenever i called Thread2.interrupted() method in above code it returned false

因为当阻塞/ sleep 方法抛出 InterruptedException 时,它们也会清除中断标志。

这是在 Thread.sleep() 的 javadoc 中:

Throws InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

关于java - java中interrupt()方法与线程状态的精确行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38026326/

相关文章:

java - 如何在 Java1.6 中安装证书,以便从 Caliper 工具上传有效

java - java中有中断处理程序吗?

python - 如何制作即使在 Python 中引发异常的情况下也能解锁的目录锁

c - 您如何在内存非常受限的嵌入式系统上处理大量数据传输?

java - 在文本字段上调用 ​​setPropertyDataSource 时,自定义转换器抛出 java.lang.ClassCastException

java - GWT/GPE/App Engine : How to (intelligently) find missing . jar 并将它们复制到/WEB-INF/lib 吗?

java - 使用改造将压缩图像上传到服务器

c++ - 在不调用 QThread::start() 的情况下使用 QThread 是否有意义?

java - SWT单线程问题

java - 并发 - 条件同步方法