您的位置:首页 > 其它

shutdownNow关不掉线程

2015-12-07 15:01 197 查看
@Override
public void run() {

try {
while (!Thread.interrupted()) {
while (!car.waxOn) {
car.waxOn();
}
}
} catch (InterruptedException e) {
System.out.println("waxon interrupted");
}

}

@Override
public void run() {
while (!Thread.interrupted()) {
while (!car.waxOn) {
try {

car.waxOn();

} catch (InterruptedException e) {
System.out.println("waxon interrupted");
}
}
}
}


上下两个run方法比较,while (!Thread.interrupted()) 判断应该放在try代码快里。否则像下面的写法,catch会吞掉InterruptedException,到时判断检测不出来以至于shutdownNow关不掉线程

shutdownNow的原理是为每个线程调用 interruput方法,然后有run方法里的判断语句来检查Thread.interrupted()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线程