如果在sleep状态下停止某一线程,会进入catch语句,并且清除停止状态值,使之变成false
class MyThread extends Thread{
@Override
public void run(){
super.run();
try{
System.out.println("run begin");
Thread.sleep(200000);
System.out.println("run end");
}catch(InterruptedException e){
System.out.println("在沉睡中被停止,进入catch! ---" +this.isInterrupted());
e.printStackTrace();
}
}
}
public class Run_sleep_interrupt{
public static void main(String [] args){
try{
MyThread thread = new MyThread();
thread.start();
thread.sleep(200);
thread.interrupt();
}catch(InterruptedException e){
System.out.println("main catch");
e.printStackTrace();
}
System.out.println("end!");
}
}
//运行结果:
// run begin
// end!
// 在沉睡中被停止,进入catch! ---false
// java.lang.InterruptedExcption: sleep interrupted at java.lang.Thread.sleep interrupted
// at Mythread.run(Run_sleep_interrupt.java:7)
//如果在sleep状态下停止某一线程,会进入catch语句,并且清除停止状态值,使之变成false