線程停止.interrupt

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();
	}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章