package com.freeflying.thread.base;
/**
* @ClassName: StopThreadAtSleep
* @Description:在休眠状态下停止线程(先停止,再休眠)
* @author freeflying
* @date 2018年6月23日
*/
public class StopThreadAtSleepA {
public static void main(String[] args) {
StopThreadAtSleepAEx stopThreadAtSleepEx = new StopThreadAtSleepAEx();
stopThreadAtSleepEx.start();
stopThreadAtSleepEx.interrupt();
System.out.println("end!!!");
}
}
class StopThreadAtSleepAEx extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 100000; i++) {
System.out.println("i=" + (i + 1));
}
System.out.println("run begin!!");
Thread.sleep(200000);
System.out.println("run end!!!");
} catch (InterruptedException e) {
System.out.println("stop thread, at sleep,run in catch method ");
e.printStackTrace();
}
}
}
结果:
i=99999
i=100000
run begin!!
stop thread, at sleep,run in catch method
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.freeflying.thread.base.StopThreadAtSleepAEx.run(StopThreadAtSleepA.java:27)