Question: Hi  

Is the class below threadsafe?

```
class ImmutablePossiblyThreadsafeClass<K, V> {

    private final Map<K, V> map;

    public ImmutablePossiblyThreadsafeClass(final Map<K, V> map) {
        this.map = new HashMap<K, V>();

        for (Entry<K, V> entry : map.entrySet()) {
            this.map.put(entry.getKey(), entry.getValue());
        }
    }

    public V get(K key) {
        return this.map.get(key);
    }
}

```

Answer: 
Yes and no, for different values of "thread safe" (sorry for the obtuse answer). The construction is not thread safe, but once the constructor completes, and assuming that has happened correctly, it will be immutable and threadsafe.

During construction, the `map` paramater may be modified by another thread between the call to `map.entrySet` and the calls to `entry.getKey` and `entry.getValue`. From the javadoc of `Map.entrySet`. This can result in undefined behaviour.

From the `java.util.Map` javadoc for `entrySet`:

> 
> Returns a set view of the mappings
>  contained in this map. Each element in
>  the returned set is a Map.Entry. The
>  set is backed by the map, so changes
>  to the map are reflected in the set,
>  and vice-versa. If the map is modified
>  while an iteration over the set is in
>  progress, the results of the iteration
>  are undefined. The set supports
>  element removal, which removes the
>  corresponding mapping from the map,
>  via the Iterator.remove, Set.remove,
>  removeAll, retainAll and clear
>  operations. It does not support the
>  add or addAll operations.
> 
> 
> 

So depending on who has a reference to the map, and how they manipulate it across threads, and how long it takes to copy the elements, you have undefined behaviour, and possibly different behaviour on different executions of the program.

There seems to be little you can do about ensuring it is constructed correctly *from within this class* (since, for example, `ConcurrentHashMap` has a similar problem). If the rest of your code can ensure that the constructor is given a thread-safe map, it will also be thread safe, but that's a wider scope than you asked.

Once, however, the constructor completes, and it is safely published[1] it will be immutable and thread safe - though only in a shallow way - if the keys or entries are mutable, this *taints* your class, making it neither immutable nor thread-safe. So, if your keys are `String`'s and your values are `Integer`'s, it will be immutable and thus thread safe. If however, your keys are a bean-like object with setters, and your values are other mutable collection types, then your class is not immutable. I tend to label this a "turtles all the way down" condition.

Essentially, your class **could be** immutable and thread safe, under certain, out-of-scope conditions not covered in your question.

[1] Your class will currently be published safely, but if, for instance, it becomes a nested class, or you pass the `this` reference to another method during the constructor, this can become an unsafe publication.
Use `ConcurrentHashMap` instead of `HashMap` and `ConcurrentMap` when declaring field and it should be.