Why can't Thread.interrupt() interrupt a thread trying to acquire lock

拟墨画扇 提交于 2019-12-01 17:45:57

A blocking operation can be interrupted only if it is declared to throw InterruptedException. Clearly, a synchronized block does not declare it, therefore it is impossible to interrupt a thread while it is waiting to acquire a lock.

Alternatively you can use an explicit lock and call Lock.lockInterruptibly().

The book is wrong, unless it is only referring to the synchronized keyword. Object.wait() throws InterruptedException.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!