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

后端 未结 9 1056
暖寄归人
暖寄归人 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<String, String> aMap = ImmutableMap.of(
        "1", "a",
        "2", "b",
        "3", "c");
    
    0 讨论(0)
  • 2020-11-29 08:54

    As already pointed out it's absolutely thread-safe, and final is important here due to its memory visibility effects.

    Presence of final guarantees that other threads would see values in the map after constructor finished without any external synchronization. Without final it cannot be guaranteed in all cases, and you would need to use safe publication idioms when making newly constructed object available to other threads, namely (from Java Concurrency in Practice):

    • Initializing an object reference from a static initializer;
    • Storing a reference to it into a volatile field or AtomicReference;
    • Storing a reference to it into a final field of a properly constructed object; or
    • Storing a reference to it into a field that is properly guarded by a lock.
    0 讨论(0)
  • 2020-11-29 08:55

    Yes it is. There is no way to modify the reference aMap itself, or add to the map after the constructor (barring reflection).

    If you expose aMap it will not be, because two threads could then modify the map at the same time.

    You could improve your class by making aMap unmodifiable via Collections.unmodifiableCollection or Collections.unmodifiableMap.

    0 讨论(0)
  • 2020-11-29 08:58

    This class has no concurrency issue cause you expose only a get method. If you add some method that modify the map you have to mark this method as synchronized.

    0 讨论(0)
  • 2020-11-29 09:07

    Yes it is, provided this is the entire class definition and not a snippet thereof.

    The key fact is that there is no way the contents of aMap can be modified post-construction.

    0 讨论(0)
  • As it is now it should be thread-safe. However if you add other methods which modify the hashmap then no.

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