[英]Java: synchronized method exception and write visibility
If a synchronized method throws an exception, do member writes made in it prior to that throw become visible to other threads? 如果synchronized方法抛出异常,那么在该抛出之前在其中创建的成员写入是否对其他线程可见? Eg
例如
class Foo
{
private int x;
public synchronized void foo()
{
x++;
// some other code using x that throws RTE for a specific thread
}
}
If an object of Foo is shared between threads t1 and t2, and t1 throws an exception as shown, in that case will the latest value of x be flushed to main memory so that it's visible to t2 when it enters foo for that object? 如果在线程t1和t2之间共享Foo的对象,并且t1如图所示抛出异常,那么将x的最新值刷新到主存储器,以便当它为该对象输入foo时它对t2可见?
Yes, changes will still be visible - you're still exiting the synchronized block, releasing the monitor in the process, and the memory model doesn't care how that occurs - just that it does occur. 是的,更改仍然可见 - 您仍然退出同步块,在此过程中释放监视器,而内存模型并不关心它是如何发生的 - 只是确实发生了。 It's still an "unlock action on a monitor" in the terms of JLS section 17.4.4 , so it synchronizes with the next action on the same monitor.
它仍然是JLS第17.4.4节中的“在监视器上解锁操作”,因此它与同一监视器上的下一个动作同步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.