"safe publication of java.util.concurrent collections" Code Answer

2

volatile is not redundant as you are changing the reference to the map. i.e. concurrentmap only provides guarentees about the contents of the collection, not references to it.

an alternative would be

public class test {
    private final map<string, string> map = new concurrenthashmap<>();

    public void resetmap() { map.clear(); }

    public map<string, string> getmap() { return map; }
}

how about other thread safe collections in java.util.concurrent (copyonwritearraylist, etc.)?

only the behaviour of the collection is thread safe. reference to the collection are not thread safe, elements in the collection are not made thread safe by adding them to the collection.

By Zack Huston on March 14 2022

Answers related to “safe publication of java.util.concurrent collections”

Only authorized users can answer the Search term. Please sign in first, or register a free account.