码迷,mamicode.com
首页 > 编程语言 > 详细

多线程——interrupt方法

时间:2018-10-27 17:41:07      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:当前时间   author   rac   test   catch   col   extend   color   ted   

测试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

 

多线程——interrupt方法

标签:当前时间   author   rac   test   catch   col   extend   color   ted   

原文地址:https://www.cnblogs.com/whx20100101/p/9862341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!