所以除了设置状态标志,还有什么退出线程的方法呢???
在catch中再次调用interrupt()就OK了
也可以在catch中写一个 break;
在catch中加入interruput()后,有点不太明白,退出是能退出,但是Thread running少打印了一次啊
package imooc.concurrent;
public class WrongWayStopThread extends Thread {
public static void main(String[] args) {
WrongWayStopThread thread = new WrongWayStopThread();
System.out.println("Staring thread...");
thread.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Interrupting thread...");
thread.interrupt();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.keepRunning=false;
System.out.println("Stopping application...");
}
public volatile boolean keepRunning = true;
public void run() {
while (keepRunning) {
System.out.println("Thread is running...");
long time = System.currentTimeMillis();
while (System.currentTimeMillis() - time < 1000) {
}
}
}
}
还有就是用try{}catch{}把while循环包起来