可能还会有人提出,我可以用中断来退出线程! 我只能说:Too Young Too Simple!中断并不会使得线程结束而退出,中断(interrupt)只是唤醒被阻塞的线程而已。
本篇,我们就来好好的聊聊:线程中断,以及如何正确的使用线程中断,和正确的线程退出。
二、为何 Thread.stop 被废弃
This method is inherently unsafe. Stopping a thread with Thread.stop causes it to unlock all of the monitors that it
has locked (as a natural consequence of the unchecked ThreadDeath exception propagating up the stack). If any of the
objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to
other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply
modifies some variable to indicate that the target thread should stop running. The target thread should check this
variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop
running. If the target thread waits for long periods (on a condition variable, for example), the interrupt method
should be used to interrupt the wait.
以上是官方 JDK 中的源码注释说明,其含义如下:
**Thread.stop 方法天生就不安全。**使用该方法来停止线程,将会导致其它因为监视器锁『监视器我们在 synchronized 中就讲过,是 Java 的内置锁』而被锁住的线程全部都解锁!(本质的后果是:没有检查的 ThreadDeath 异常会在栈中传播,因而使得监视器锁解锁)。如果任何一个被监视器锁给锁住的对象处于一个不一致的状态,那么其被解锁后将会被其它线程可见,潜在的结果是产生任何后果。**我们应该使用一个变量来代替使用 stop 方法,告诉目标线程退出『这里就是我们开头所说的第一种方法,设置一个标志位』。**目标线程应该周期性的检查这个变量,并根据这个变量来正确的退出 run 方法。如果目标线程处于阻塞/休眠状态(如:使用 wait、sleep、yield 方法后,线程让出了 CPU 使用权,进而阻塞/休眠),此时,该标志位变量将不会起作用,那么,应该使用 interrupt 方法来中断目标线程的阻塞/休眠状态,将其唤醒!
public class InterruptDemo implements Runnable {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
long timestamp = System.currentTimeMillis();
timestamp = System.currentTimeMillis() - timestamp;
System.out.println("Thread run, total sleep = " + timestamp + "(ms)");
}
System.out.println("Thread exit");
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new InterruptDemo(), "InterruptDemo");
System.out.println("start thread");
thread.start();
Thread.sleep(100);
System.out.println("interrupt thread");
thread.interrupt();
System.out.println("main exit");
}
}
输出结果:
start thread
.......
Thread run, total sleep = 0(ms)
interrupt thread
Thread run, total sleep = 0(ms)
Thread run, total sleep = 0(ms)
Thread run, total sleep = 0(ms)
main exit
Thread exit
3.5、标志位 + 线程中断结合
public class InterruptDemo implements Runnable {
private static final AtomicBoolean running = new AtomicBoolean(true);
@Override
public void run() {
while (running.get()) {
System.out.println("Thread will sleep 10s ------------------------- running");
long timestamp = System.currentTimeMillis();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
System.out.println("Interrupted... Todo other things then exit......");
running.set(false);
continue;
}
timestamp = System.currentTimeMillis() - timestamp;
System.out.println("Thread run, total sleep = " + timestamp + "(ms)");
}
System.out.println("Thread exit");
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new InterruptDemo(), "InterruptDemo");
System.out.println("start thread");
thread.start();
Thread.sleep(3000);
System.out.println("interrupt thread");
thread.interrupt();
System.out.println("main exit");
}
}
输出结果:
start thread
Thread will sleep 10s ------------------------- running
interrupt thread
main exit
Interrupted... Todo other things then exit......
Thread exit