1.异常法:
package test; import exthread.MyThread; import exthread.MyThread; public class Run { public static void main(String[] args) { try { MyThread thread = new MyThread(); thread.start(); Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end!"); } }
package exthread; public class MyThread extends Thread { <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void run() { <span style="white-space:pre"> </span>super.run(); <span style="white-space:pre"> </span>for (int i = 0; i < 500000; i++) { <span style="white-space:pre"> </span>if (this.interrupted()) { <span style="white-space:pre"> </span>System.out.println("已经是停止状态了!我要退出了!"); <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>System.out.println("i=" + (i + 1)); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>System.out.println("我被输出,如果此代码是for又继续运行,线程并未停止!"); <span style="white-space:pre"> </span>} }
运行结果:
......
i=255229
i=255230
i=255231
i=255232
i=255233
i=255234
i=255235
i=255236
i=255237
i=255238
i=255239
i=255240
i=255241
i=255242
i=255243
i=255244
i=255245
i=255246
i=255247
i=255248
i=255249
i=255250
i=255251
i=255252
i=255253
i=255254
i=255255
i=255256
i=255257
i=255258
i=255259
i=255260
i=255261
i=255262
i=255263
i=255264
i=255265
i=255266
已经是停止状态了!我要退出了!
end!
我被输出,如果此代码是for又继续运行,线程并未停止!
已经是停止状态了!我要退出了! 虽然停止,但是for循环以后的代码还是会执行的
正确的退出方式
package exthread; public class MyThread extends Thread { @Override public void run() { super.run(); try { for (int i = 0; i < 500000; i++) { if (this.interrupted()) { System.out.println("已经是停止状态了!我要退出了!"); throw new InterruptedException(); } System.out.println("i=" + (i + 1)); } System.out.println("我在for下面"); } catch (InterruptedException e) { System.out.println("进MyThread.java类run方法中的catch了!"); e.printStackTrace(); } } }
2.在沉睡中停止
package exthread; public 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(); } } }
package test; import exthread.MyThread; import exthread.MyThread; public class Run { 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.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at exthread.MyThread.run(MyThread.java:9)
另外一种情况是先停止后sleep
3.暴力停止:
Thread.stop() 这种方法存在非线程安全问题。所以暂时不考虑
4.使用return 停止线程
package test.run; import extthread.MyThread; public class Run { public static void main(String[] args) throws InterruptedException { MyThread t=new MyThread(); t.start(); Thread.sleep(2000); t.interrupt(); } }
package extthread; public class MyThread extends Thread { @Override public void run() { while (true) { if (this.isInterrupted()) { System.out.println("停止了!"); return; } System.out.println("timer=" + System.currentTimeMillis()); } } }