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
原文:https://www.cnblogs.com/whx20100101/p/9862341.html