ITeye博客

interrupt 终止线程

dmwdmc · 2013-06-17 21:27 · 线程 · 编程语言 · Markdown


public class SleepThread extends Thread{
	@Override
	public void run() {
		try {
			while(true){
				System.out.println("sleep start");
				Thread.sleep(10000);
				System.out.println("sleep end");
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
public class ThreadTest {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Thread t1=new SleepThread();
		t1.start();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t1.interrupt();
	}

}

运行:

sleep start java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at SleepThread.run(SleepThread.java:9)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。