假设我在一个方法中有这段代码:
...
synchronized (this) {
this.var = value;
}
...
是否有任何情况使 synchronized
block 抛出异常(假设 this
、var
和 value
有非 null
值)?
谢谢!
最佳答案
根据Java Language Specification about the synchronized statement :
The executing thread locks the monitor associated with V. Then the Block is executed [...]
还有 Java virtual Machine Specification on Synchronization ,这解释了如何使用所提到的监视器,只讨论从同步块(synchronized block)中抛出的异常。所以 synchronized
语句本身不允许抛出异常。
因此,在您的情况下,不会出现任何抛出异常的情况。
关于java - 简单的 Java 同步块(synchronized block)可以抛出异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44943618/