java-memory-model

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

烂漫一生 提交于 2019-11-26 09:47:35
问题 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

Is Dalvik&#39;s memory model the same as Java&#39;s?

我与影子孤独终老i 提交于 2019-11-26 09:20:17
问题 Is Dalvik\'s memory model the same as Java\'s? I am particularly interested in whether reads and writes of reference and non- long /non- double primitive variables are atomic, but I would also like to know whether there are any differences between the two platforms\' memory models. 回答1: As of 4.0 (Ice Cream Sandwich), Dalvik's behavior should match up with JSR-133 (the Java Memory Model). As of 3.0 (Honeycomb), most of the pieces were in place, but some minor things had been overlooked that

Must all properties of an immutable object be final?

南楼画角 提交于 2019-11-26 04:20:14
问题 Must immutable objects have all properties be final ? According to me not. But I don\'t know, whether I am right. 回答1: The main difference between an immutable object (all properties final) and an effectively immutable object (properties aren't final but can't be changed) is safe publication. You can safely publish an immutable object in a multi threaded context without having to worry about adding synchronization, thanks to the guarantees provided by the Java Memory Model for final fields: