中断线程最好的,最受推荐的方式是,使用共享变量(shared variable)发出信号,告诉线程必须停止正在运行的任务。与interrupt方法配合
private class TimerDownThread extends Thread {
@Override
public void run() {
isStart = true;
while (isStart)
while (!isStop)
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
将isStop设置为true,然后使用interrupt方法终端睡眠即可暂停线程
再将isStop设置为false即可启动线程
想要结束线程把isStart设置为false即可