Skip to content
New issue

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

volatile保证原子性 #143

Open
Hewei-ss opened this issue May 11, 2024 · 1 comment
Open

volatile保证原子性 #143

Hewei-ss opened this issue May 11, 2024 · 1 comment

Comments

@Hewei-ss
Copy link

章节
第八章 volatile

相关截图或文字
屏幕截图 2024-05-11 232638
image

疑问
对单个volatile修饰的变量的写操作,第一步:JMM会先将这个变量写到线程本地缓存中,第二步:再将这个变量刷新到主存中。这是两步,为什么说volatile能保证对单个volatile变量的读/写具有原子性???

@gongxuanzhang
Copy link

@Hewei-ss
因为volatile有内存屏障,对于单一变量的读写,只要保证对这个变量的读写永远是最新的即可

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants