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
  • 相关阅读:
    词法分析器实验报告(JAVA)
    词法编辑器(Java)
    编译原理的那些事
    Discuz7.2 faq.php页面注入漏洞分析
    Discuz7.2 XML漏洞
    Python 爬取广州商学院新闻----测试版
    进程调度
    DOS下的网络管理命令
    DOS批处理实验
    熟悉使用DOS操作命令
  • 原文地址:https://www.cnblogs.com/whx20100101/p/9862341.html
Copyright © 2011-2022 走看看