Can anyone tell me whether this class is threadsafe or not ?
class Foo {
private final Map<String,String> aMap;
public Foo() {
aMap = new HashMap<String, String>();
aMap.put("1", "a");
aMap.put("2", "b");
aMap.put("3", "c");
}
public String get(String key) {
return aMap.get(key);
}
}
Edit: It my fault to not clarify the question. According to JMM FAQ :
A new guarantee of initialization safety should be provided. If an object is properly constructed (which means that references to it do not escape during construction), then all threads which see a reference to that object will also see the values for its final fields that were set in the constructor, without the need for synchronization.
This made me confuse that the set to aMap is aMap = new HashMap<String, String>();
. So other threads can see these
aMap.put("1", "a");
aMap.put("2", "b");
aMap.put("3", "c");
or not ?
Edit: I found this question that exactly closed to my question