Java synchronized 保证可见性
Java juc 锁 About 743 words原因
synchronized
在代码块执行结束后会将执行结果刷新到主存中。
所以synchronized
也和volatile
有可见性的作用。
JMM 规范
synchronized
在可见性上和volatile
作用一致。
Doug Lea
原文节选:
In terms of atomicity, visibility, and ordering, declaring a field as volatile is nearly identical in effect to using a little fully synchronized class protecting only that field via get/set methods, as in: Declaring a field as volatile differs only in that no locking is involved. In particular, composite read/write operations such as the "++'' operation on volatile variables are not performed atomically.
final class VFloat {
private float value;
final synchronized void set(float f) { value = f; }
final synchronized float get() { return value; }
}
引用段落出处
Views: 1,899 · Posted: 2021-09-03
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...