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: 
There's a potential problem when the constructor parameter's contents change while you're copying them. But there isn't really anything you can do to prevent that. Another potential problem would be with mutable keys.

Also, you can replace the constructor with this:

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

```
I think it is thread-safe the way it is. You've created (effectively) a copy of the input `map` and are doing only read-only operations on it. I've sort of always held the belief that immutable classes are inherently thread-safe. But be aware that unless your `K` and `V` classes are immutable, you could still run into thread safety issues.

You could also replace it with:

```
Map<K, V> m = Collections.unmodifiableMap(Collections.synchronizedMap(map));

```