zoukankan      html  css  js  c++  java
  • 多线程——interrupt方法

    测试interrupt()方法:

    package day_12_01_Thread;
    
    import java.util.Date;
    
    /**
     * 测试interrupt()方法:结束线程,但是线程还是活着的
     * 
     * @author Administrator
     *
     */
    public class MyThreadSleep {
        public static void main(String[] args) {
            TestThreadSleep testThreadSleep = new TestThreadSleep();
            testThreadSleep.start();
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            testThreadSleep.interrupt();
            System.out.println(testThreadSleep.isAlive());
        }
    }
    
    class TestThreadSleep extends Thread {
        public void run() {
            while (true) {
                System.out.println("当前时间:" + new Date());
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    return;
                }
            }
        }
    }
    结果:
    当前时间:Mon May 14 13:05:56 CST 2018
    当前时间:Mon May 14 13:05:59 CST 2018
    当前时间:Mon May 14 13:06:00 CST 2018
    当前时间:Mon May 14 13:06:01 CST 2018
    当前时间:Mon May 14 13:06:02 CST 2018
    当前时间:Mon May 14 13:06:03 CST 2018
    当前时间:Mon May 14 13:06:04 CST 2018
    当前时间:Mon May 14 13:06:05 CST 2018
    当前时间:Mon May 14 13:06:06 CST 2018
    true
  • 相关阅读:
    python之路
    go mod
    黑苹果流程
    mac go配置,环境配置
    mac重装系统
    多级分销概念 MongoDB||MySQL
    MongoDB查询mgov2的聚合方法
    linux被当矿机排查案例
    docker-compose容器中redis权限问题
    docker-compose中redis查询版本
  • 原文地址:https://www.cnblogs.com/whx20100101/p/9862341.html
Copyright © 2011-2022 走看看