使用volatile布尔变量作为标志位/interrupt方法中断线程。在线程的run方法中while检查标志位\中断状态
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {//响应中断
// 执行任务
}
} catch (InterruptedException e) {
// 线程被中断时的清理代码
} finally {
// 线程结束前的清理代码
}
}
使用volatile布尔变量作为标志位/interrupt方法中断线程。在线程的run方法中while检查标志位\中断状态
public void run() {
try {
while (!Thread.currentThread().isInterrupted()) {//响应中断
// 执行任务
}
} catch (InterruptedException e) {
// 线程被中断时的清理代码
} finally {
// 线程结束前的清理代码
}
}

被折叠的 条评论
为什么被折叠?