Java concurrency: is final field (initialized in constructor) thread-safe?

后端 未结 9 1079
暖寄归人
暖寄归人 2020-11-29 08:35

Can anyone tell me whether this class is threadsafe or not ?

class Foo {

    private final Map aMap;

    public Foo() {
        aMap =         


        
9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 08:52

    Guava has immutable classes for making this sort of thing easier and guaranteed immutable:

    private final ImmutableMap aMap = ImmutableMap.of(
        "1", "a",
        "2", "b",
        "3", "c");
    

提交回复
热议问题