Why can an Object member variable not be both final and volatile in Java?

后端 未结 7 1214
我寻月下人不归
我寻月下人不归 2020-12-24 05:37

If in a class I have a ConcurrentHashMap instance that will be modified and read by multiple threads I might define like this:

public class My Class {

    p         


        
相关标签:
7条回答
  • 2020-12-24 06:15

    volatile modifier guarantees that all reads and writes go strait to main memory, i.e. like the variable access is almost into synchronized block. This is irrelevant for final variable that cannot be changed.

    0 讨论(0)
提交回复
热议问题