gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:16 25 4
gpt4 key购买 nike

我已阅读下面的帖子

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com