We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
老哥,这副图中的线程a和线程b共同访问的变量a(没有volatile关键字)也能保证同步么?
The text was updated successfully, but these errors were encountered:
可以保证。可以从两个方面理解: 内存屏障: volatile关键字会在volatile写之前插入StoreStore,保证volatile写之前的内容先写入主内存,保证对所有线程可见(禁止a写和flag写的重排序);而volatile读后会插入LoadLoad和LoadStore,保证在读取变量a前,先读取flag(禁止a读和flag读重排序)。所以就能保证它们的可见性。
happens-before: 作者的代码没看,你可以看我的理解一下 /捂脸
最后根据传递性规则,① happens-before ④
Sorry, something went wrong.
懂了,谢谢老哥指点
老哥还想在问一下细节 JMM的内存屏障只是用来确保对volatile对象的读写操作不会发生重排序。 但是真正volatile关键字真正用来同步线程内存和主内存的操作是原文说的"Lock前缀的指令"这个作用吧,这个理解对么? (顺便问一下为啥我javap 看指令操作时没有发现一个Lock开头的指令。。)
lock开头的指令是在JIT的汇编代码里 参考周志明的《深入Java虚拟机》P111
No branches or pull requests
老哥,这副图中的线程a和线程b共同访问的变量a(没有volatile关键字)也能保证同步么?
The text was updated successfully, but these errors were encountered: