Thinking in Java 一书中写道,Thread.interrupt()
不能中断正在尝试获取同步锁的线程,我想知道为什么?
最佳答案
只有声明抛出 InterruptedException
时,阻塞操作才能被中断。显然,synchronized
block 没有声明它,因此不可能在线程等待获取锁时中断它。
或者,您可以使用显式锁并调用 Lock.lockInterruptibly()
。
关于java - 为什么 Thread.interrupt() 不能中断试图获取锁的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32024436/