【发布时间】:2020-03-30 16:15:35
【问题描述】:
我正在尝试下面的代码,我中断了一个用户线程,当我打印 isInterrupted 的值时它返回 false,我没有得到一个 true 值,这里标志将在异常发生时重置被捕获或调用中断的方法。
其次,根据我的理解,sleep方法应该在每次迭代中抛出和interruptedException,catch print中的值,但它只抛出一次。
class ThreadInterruptt extends Thread
{
public void run()
{
for(int i = 0; i<100;i++)
{
try {
Thread.sleep(1000);
System.out.println(i);
System.out.println(isInterrupted());
} catch (InterruptedException e) {
System.out.println("Thread Interrupted");
}
}
}
}
public class ThreadInterrupt {
public static void main(String ag[]) throws InterruptedException
{
ThreadInterruptt t = new ThreadInterruptt();
t.start();
Thread.sleep(100);
t.interrupt();
}
}
【问题讨论】:
标签: java multithreading concurrency java-threads