class MyThread implements Runnable {
@Override
public void run() {
System.out.println("1、进入run()方法休眠");
try {
System.out.println("2、线程休眠20秒");
Thread.sleep(20000);//这里休眠20秒
System.out.println("3、线程正常休眠完毕");
} catch (InterruptedException e) {
System.out.println("4、线程发生异常休眠被中断");
return;//返回方法调用处
}
System.out.println("5、线程正常结束run()方法体");
}
}
public class InterruptDemo {
public static void main(String[] args) {
MyThread mt = new MyThread();
Thread t = new Thread(mt,"线程A");
t.start();//启动线程
//========================================================
try {
Thread.sleep(2000); //保证线程至少执行2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
//========================================================
t.interrupt();//中断线程
}
}