Related Java will synchronized block if changed?
public class TestConcurrentTwo {
public static void main(String[] args) {
class lock {
public Object lockObj = new Object();
int protectedValue = 0;
}
lock a = new lock();
Thread t = new Thread(() -> {
try {
synchronized (a.lockObj) {
System.out.println("from first block start");
System.out.println(a.protectedValue);
a.lockObj = new Object();
Thread.sleep(1000);
System.out.println(a.protectedValue);
System.out.println("from first block done");
}
} catch (InterruptedException x) {}
});
t.start();
try {
Thread.sleep(100);
} catch (InterruptedException x) {}
synchronized (a.lockObj) {
System.out.println("from second block start");
a.protectedValue++;
System.out.println("from second block done");
}
System.out.println(a.protectedValue);
}
}
OUTPUT:
from first block start
0
from second block start
from second block done
1
1
from first block done
In the related answer, they both hold the original reference so the second block should be blocked until the completion of the first.
However, second block indeed proceeded once a.lockObj changed from first block. why?
a.lockObja reference; theObjectreferenced bya.lockObjis the referenced object. The synchronization depends on the referenced object, not on the reference. There are two different referenced objects being used for synchronization; result misery.