zoukankan      html  css  js  c++  java
  • 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,子线程就被暴力停止了

  • 相关阅读:
    iptables服务器主机防火墙
    VMware克隆Linux虚拟机报错
    CentOS7.3下yum安装MariaDB10.3.12并指定utf8字符集
    CentOS7.3yum安装MariaDB报错[Errno 256]
    [LeetCode] 121. Best Time to Buy and Sell Stock
    [LeetCode] 116. Populating Next Right Pointers in Each Node
    [LeetCode] 113. Path Sum II
    jQuery实现图片添加及预览
    H5移动端适配——解决移动端必须手动调整以适配的问题
    [LeetCode] 110. Balanced Binary Tree
  • 原文地址:https://www.cnblogs.com/edison20161121/p/7954758.html
Copyright © 2011-2022 走看看