【发布时间】:2017-11-11 12:48:17
【问题描述】:
java中的线程在被中断方法中断之前是否必须处于就绪状态? 我试图通过在下面输入上面给出的代码来检查这一点。
class MyThread extends Thread
{
public void run() {
try
{
for(int i =0;i<10;i++) {
System.out.println("I am lazy thread");
Thread.sleep(2000);
}
}
catch(InterruptedException e) {
System.out.println("I got interrupted");
}
}
}
public class SleepAndInterruptDemonstrationByDurga {
public static void main(String[] args) {
MyThread t= new MyThread();
t.start();
t.interrupt();
System.out.println("End of main thread");
}
}
即使尝试了很多次,我得到的输出始终是下面的输出
End of main thread
I am lazy thread
I got interrupted
为什么不能输出
I am lazy thread
I got interrupted
End of main thread
从代码可以看出,中断方法是由主线程首先调用的。最后我想问一下,在线程启动之前首先执行中断调用时,是否有任何可能的情况?
【问题讨论】:
标签: java multithreading interrupt interruption