使用stop方式暴力停止线程
1 package com.cky.thread; 2 3 /** 4 * Created by edison on 2017/12/3. 5 */ 6 public class MyThread extends Thread{ 7 private int i=0; 8 @Override 9 public void run() { 10 super.run(); 11 try { 12 while(true) { 13 i++; 14 System.out.println("i="+i); 15 Thread.sleep(1000); 16 } 17 } catch (InterruptedException e) { 18 e.printStackTrace(); 19 } 20 } 21 }
package com.cky.test; import com.cky.thread.MyThread; /** * Created by edison on 2017/12/3. */ public class Test { public static void main(String[] args) { try { MyThread th = new MyThread(); th.start(); Thread.sleep(8000); th.stop(); } catch (InterruptedException e) { e.printStackTrace(); } } }
C:itsoftjdkinjava -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib esources.jar;C:itsoftjdkjrelib t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 Process finished with exit code 0
结果分析,当i只执行到8,子线程就被暴力停止了