Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

AQS详解中关于成员变量state的volatile修饰的功能的解释错误 #2516

Open
zxpzxpzxpzxpzxp opened this issue Oct 31, 2024 · 0 comments

Comments

@zxpzxpzxpzxpzxp
Copy link

AQS详解中,介绍AQS的成员变量state由 volatile 修饰,其中volatile的作用是使用volatile修饰保证线程可见性.
实际上使用 volatile 修饰状态变量不是为了利用 volatile 的内存可见性,因为这个状态变量只会被持有该状态变量的线程写入,只会被队列中该线程的后驱节点对应的线程读,而且后者会轮询读取。
但要实现一个可以在多线程程序中正确执行的锁,还需要解决重排序问题。主要是通过 volatile 的 Happens-Before(先行发生)规则来解决重排序问题。JMM 的 Happens-Before(先行发生)规则有一条针对 volatile 关键字的规则:“volatile 变量的写操作发生在该变量的后续读之前”。

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

No branches or pull requests

1 participant