java中如何中断一个正在运行的线程?
stop方法已经被弃用了,interrupt方法可以被用来请求中断一个线程。但是在使用中有个误区:
package concurrent;
public class InterruptedTest implements Runnable{
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Thread t = new Thread(new InterruptedTest(),"myThread");
t.start();
Thread.sleep(3000);
t.interrupt();
}
@Override
public void run() {
while(!Thread.interrupted()){
System.out.println(Thread.currentThread().getName() + " Running..";
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Some one Interrupted me!");
}
}
System.out.println("Exit thread "+Thread.currentThread().getName());
}
}
在这个例子中,我们通过在while循环中判断该线程的中断状态来决定线程是否继续运行,主线程中,我们等待3s后给子线程发送一个interrupt消息,来期望打断子线程。但是并没有达到效果:
myThread Running..
myThread Running..
myThread Running..
Some one Interrupted me!
myThread Running..
myThread Running..
myThread Running..
myThread Running..
...
可以看到,在使用interrupt方法后,子线程捕获到了InterruptedException,然后线程继续运行。线程居然没被打断!这是为什么呢?
jdk文档中关于interrupt方法的说明:
如果线程在调用Object类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)
sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException
可以看到,如果在上述几种阻塞方法中调用了interrupt方法,它会干两件事:1.发送一个InterruptedException,2.将中断状态重置为false。在上面的例子中我们使用了sleep方法,因此,它会捕获到异常,同时线程的中断状态重置为0,因此,while循环永远为真。线程就不会中断。
我们来验证一下,如果在子线程中不是一个sleep方法,会出现什么结果呢?
package concurrent;
public class InterruptedTest implements Runnable{
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Thread t = new Thread(new InterruptedTest(),"myThread");
t.start();
Thread.sleep(3000);
t.interrupt();
}
@Override
public void run() {
while(!Thread.interrupted()){
System.out.println(Thread.currentThread().getName() + " Running..");
long time = System.currentTimeMillis();
while(System.currentTimeMillis() - time < 1000)
;
/*try {
Thread.sleep(1000);
System.out.println(Thread.interrupted());
} catch (InterruptedException e) {
System.out.println("Some one Interrupted me!");
}*/
}
System.out.println("Exit thread "+Thread.currentThread().getName());
}
}
myThread Running..
myThread Running..
myThread Running..
Exit thread myThread
使用当前时间减去开始时间来模拟线程休眠1s钟,这样就避免使用sleep方法,interrupt方法就不会重置中断状态,线程正常结束
了解了这种情况,就可以通过以下两种方法来进行改进:
- 在catch中重新将中断状态设置为true
catch (InterruptedException e) {
System.out.println("Some one Interrupted me!");
Thread.currentThread().interrupt();
}
- 使用 throws InterruptedException来标记你的方法,而不采用try catch,于是,调用者可以捕获这一个异常:
package javatraps;
public class InterruptedTest implements Runnable {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new InterruptedTest(), "myThread");
t.start();
Thread.sleep(3000);
t.interrupt();
}
public void func() throws InterruptedException {
while (!Thread.interrupted()) {
System.out.println(Thread.currentThread().getName() + " Running..");
Thread.sleep(1000);
}
}
@Override
public void run() {
try {
func();
} catch (InterruptedException e) {
System.out.println("some one interrupted me!");
}
System.out.println("Exit thread " + Thread.currentThread().getName());
}
}
myThread Running..
myThread Running..
myThread Running..
myThread Running..
some one interrupted me!
Exit thread myThread