• 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: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,子线程就被暴力停止了

  • 相关阅读:
    又是一年叶落时(二)
    动态规划 之 区间DP练习
    [hdu2255] 奔小康赚大钱
    [洛谷P1967] 货车运输
    [UVA1494] Qin Shi Huang's National Road System
    斜率优化总结
    latex一些有用的写法
    [YTU]_2384 ( 矩形类中运算符重载【C++】)
    [YTU]_2442( C++习题 矩阵求和--重载运算符)
    [YTU]_2640( 编程题:运算符重载---矩阵求和)
  • 原文地址:https://www.cnblogs.com/edison20161121/p/7954758.html
Copyright © 2020-2023  润新知