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

Why is `synchronized (new Object()) {}` a no-op?

The FAQ is not the authority on the matter; the JLS is. Section 17.4.4 specifies synchronizes-with relationships, which feed into happens-before relationships (17.4.5). The relevant bullet point is: An unlock action on monitor m synchronizes-with all subsequent lock actions on m (where “subsequent” is defined according to the synchronization order). Since m here is the … 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

Why does this Java program terminate despite that apparently it shouldn’t (and didn’t)?

Obviously the write to currentPos doesn’t happen-before the read of it, but I don’t see how that can be the issue. currentPos = new Point(currentPos.x+1, currentPos.y+1); does a few things, including writing default values to x and y (0) and then writing their initial values in the constructor. Since your object is not safely published … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)