最后的思考题
所以除了设置状态标志,还有什么退出线程的方法呢???
所以除了设置状态标志,还有什么退出线程的方法呢???
2018-07-17
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) {
}
}
}
}
举报