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

后端 未结 7 1237
我寻月下人不归
我寻月下人不归 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:09

    Because it doesn't make any sense. Volatile affects object reference value, not the object's fields/etc.

    In your situation (you have concurrent map) you should do the field final.

提交回复
热议问题