---
title: "interrupt 终止线程"
author: "dmwdmc"
date: "2013-06-17T21:27:51Z"
tags: []
categories: ["编程语言"]
slug: "interrupt"
draft: false
lastmod: "2013-06-17T21:28:12Z"
id: 1888397
url: "https://dmwdmc.iteye.com/blog/1888397"
---

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

```java
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)