成功的路上总是离不开贵人的帮助,名师的指点和小人的刺激。

莫怕,过了桥,就翻篇了

1.7.5能停止的线程-暴力停止

使用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:\itsoft\jdk\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_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,子线程就被暴力停止了

posted on 2017-12-03 15:38  痞子陈2016  阅读(157)  评论(0编辑  收藏  举报

导航