写过多线程的童鞋,可能都会遇到一个问题,那就是线程如何优雅的停止!这里主要介绍两个办法:标志位 和 thread.isInterrupted
1.通过 标志位 来终止线程
private static class Runner implements Runnable{
private long i;
private (volatile) boolean running =true;
@Override
public void run() {
System.out.println("current Thread Name:"+Thread.currentThread().getName());
while (running ){
i++;
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
}
}
System.out.println("Count i= "+i);
System.out.println("current Thread Name:"+Thread.currentThread().getName());
}
public void cancel(){
running =false;
System.out.println("running=false");
}
}
此程序会在 running=false 之后,继续等待 10s ,通过 标识位 并不能阻止 sleep wait 等继续执行。
所以此方法不是特别优雅
2.通过 Thread.currentThread().isInterrupted() 来判断
public class Shutdoown {
public static void main(String[] args) {
Runner one = new Runner();
Thread countThread = new Thread(one, "CountThread");
countThread.start();
try {
//保证线程已启动
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
}
countThread.interrupt();
}
private static class Runner implements Runnable {
private long i;
private volatile boolean running = true;
@Override
public void run() {
System.out.println("current Thread Name start:" + Thread.currentThread().getName());
while (!Thread.currentThread().isInterrupted()) {
System.out.println("=== " + Thread.currentThread().isInterrupted());
i++;
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("error=== " + e);
}
}
System.out.println("Count i= " + i);
System.out.println("current Thread Name stop:" + Thread.currentThread().getName());
}
}
thread 被中断之后,就可以无视 sleep wait 等方法,马上停止,乍一看,还是蛮优雅的,推荐使用。
可如果我把 sleep 换乘 while(true)会怎么样?
- 通过 Thread.currentThread().isInterrupted() 来判断-while(true) 版本
public class Shutdoown {
public static void main(String[] args) {
Runner one = new Runner();
Thread countThread = new Thread(one, "CountThread");
countThread.start();
try {
//保证线程已启动
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
}
countThread.interrupt();
}
private static class Runner implements Runnable {
private long i;
private volatile boolean running = true;
@Override
public void run() {
System.out.println("current Thread Name start:" + Thread.currentThread().getName());
while (!Thread.currentThread().isInterrupted()) {
System.out.println("=== " + Thread.currentThread().isInterrupted());
i++;
while(true){
}
}
System.out.println("Count i= " + i);
System.out.println("current Thread Name stop:" + Thread.currentThread().getName());
}
}
不要意思,除非主动 kill,不然 线程 永远停止不了。
总结:
显然,通过 Thread.currentThread().isInterrupted() 来停止线程,显然要比标示位优雅一些,但要防止条件判断永远无法生效。