java - 如果线程在 wait() 上被中断,是否会在到达同步块(synchronized block)之外的 catch block 之前重新获取监视器?

标签 java multithreading try-catch monitor interrupted-exception

请考虑以下代码:

try{
    synchronized(myLock){
        myLock.wait(); // gets interrupted while waiting.
   }
}catch(InterruptedException ie){

}

如果线程在 wait() 阻塞时被中断,myLock 的监视器是否会在进入 catch block 之前重新获取(暂时?)?

最佳答案

作为javadoc

If the current thread is interrupted by any thread before or while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.

所以,是的,它会阻塞等待重新获取锁,然后抛出异常。

关于java - 如果线程在 wait() 上被中断,是否会在到达同步块(synchronized block)之外的 catch block 之前重新获取监视器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23797852/

相关文章:

java - 如果作业使用 simpleAsyncTaskExecutor 运行,如何在 Spring Batch 中编写测试用例

java - Desktop.Action.MAIL 为 mailto : in URI 正确编码主题和正文字符串

java - getWaitQueueLength 抛出 IllegalArgumentException : Not owner

java - 在Java中,为什么要在catch block 中指定异常类型?

php - 对所有PDO查询进行一次全局尝试{}捕获{}

Java 泛型不兼容类型链表迭代器

JAVA:parseInt 问题

c# - 确保线程恰好在一秒钟内滴答作响的最佳方法是什么?

python - 如何在 python 中创建一个线程安全的全局计数器

r - doWithOneRestart 错误