在 最简单的
办法就是
interrupt()它,这将导致
Thread.currentThread().isInterrupted()返回
true,也可能抛出
InterruptedException在某些情况下,其中线程
等待 ,例如
Thread.sleep(),
otherThread.join(),
object.wait()等。
在
run()方法内部,您需要捕获该异常和/或定期检查该
Thread.currentThread().isInterrupted()值并执行某些操作(例如,细分)。
注:虽然
Thread.interrupted()看起来一样
isInterrupted(),它有一个讨厌的副作用:调用
interrupted()
清零 的
interrupted标志,而呼叫
isInterrupted()没有。
其他非中断方法包括使用
volatile正在运行的线程监视的“停止”()标志。



