Thread.stop()是一个不建议使用的方法。
Thread in Java will stop once run() method finished. Another important point is that you can not restart a Thread which run() method has finished already , you will get an IllegalStateException.
所以正确让一个线程stop的方式是使用一个volatile变量来控制。
public class StoppableThread extends Thread {
private volatile boolean stop = false;
public void stopGracefully() {
stop = true;
}
public void run() {
while (!stop) {
// long running action - finished will be true once work is done
}
}
}