Thread-safe setting of a variable (Java)?

后端 未结 5 1108
我寻月下人不归
我寻月下人不归 2020-12-11 15:59

Given the following code:

public class FooBar { 

  public static volatile ConcurrentHashMap myConfigData  = new ConcurrentHashMap();      

}

public class          


        
5条回答
  •  囚心锁ツ
    2020-12-11 16:52

    volatile guarantees atomicity, visibility and acts as a 'memory barrier' (google for it, if you want to know what that means) - at least since Java 5. Therefore it does exactly what you want.

提交回复
热议问题