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

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

    volatile is used for variables that their value may change, in certain cases, otherwise there is no need for volatile, and final means that the variable may not change, so there's no need for volatile.

    Your concurrency concerns are important, but making the HashMap volatile will not solve the problem, for handling the concurrency issues, you already use ConcurrentHashMap.

提交回复
热议问题