package com.business.thread;
public class EndThread {
private static class useThread extends Thread {
public useThread(String name) {
super(name);
}
public void run() {
//isInterrupted檢查中斷標誌位
while (!isInterrupted()) {
try {
System.out.println(Thread.currentThread().getName());
} catch (Exception e) {
//拋出異常InterruptedException的時候,再interrupt()一下
interrupt();
}
}
System.out.println(Thread.currentThread().getName()+"is interrupted");
}
}
public static void main(String[] args) throws InterruptedException {
useThread thread1 = new useThread("thread1");
thread1.start();
thread1.sleep(30);
//interrupt將中斷標誌位設置爲true
thread1.interrupt();
}
}