Is Dalvik’s memory model the same as Java’s?

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 would be difficult to encounter in practice (e.g. some edge cases in finalization). As of 2.3 (Gingerbread), Dalvik was … Read more

Instruction reordering & happens-before relationship [duplicate]

The key point of the program order rule is: in a thread. Imagine this simple program (all variables initially 0): T1: x = 5; y = 6; T2: if (y == 6) System.out.println(x); From T1’s perspective, an execution must be consistent with y being assigned after x (program order). However from T2’s perspective this does … Read more

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

As already pointed out it’s absolutely thread-safe, and final is important here due to its memory visibility effects. Presence of final guarantees that other threads would see values in the map after constructor finished without any external synchronization. Without final it cannot be guaranteed in all cases, and you would need to use safe publication … Read more

Must all properties of an immutable object be final?

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 … Read more