【发布时间】:2015-09-10 10:43:30
【问题描述】:
我正在尝试创建一个线程然后中断它。但它不会停止并导致异常。谁能解释我做错了什么?谢谢。
public class Test {
public static void main(String[] args) throws InterruptedException {
//Add your code here - добавь код тут
TestThread test = new TestThread();
test.start();
Thread.sleep(5000);
test.interrupt();
}
public static class TestThread extends Thread {
public void run() {
while (!this.isInterrupted()) {
try {
Thread.sleep(1000);
System.out.println("I did the Thread");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
【问题讨论】:
-
导致异常。什么例外?
-
好像work fine。
-
@BoristheSpider 也许当你执行它时,执行中断时线程正在运行。你跑了几次?
标签: java multithreading interrupt interrupted-exception