多線程——interrupt方法
阿新 • • 發佈:2018-10-27
ali cst exce args string extend ava trace threads
測試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 May14 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方法