Thread.holdsLock () and lock escalation

Suppose I have 2 adjacent synchronized blocks with a call to Thread.holdsLock() between them:

 final Object lock = new Object(); // ... synchronized (lock) { // do stuff } if (Thread.holdsLock(lock)) { throw new IllegalStateException(); } synchronized (lock) { // do more stuff } 

Now, what if at some point the JVM decides to coarsen the lock and merge the above synchronized blocks? Will the call to Thread.holdsLock() still return false , or will the code fail with an exception?

+6
source share
1 answer

Good question, I would say no, based on the JSR 133 Cookbook . The grid shows that a NormalLoad cannot be reordered above MonitorExit . In this case, reading will, in theory, be a lock.

In addition, Rafael Winterhalter mentions in a presentation on the Java memory model that native calls cannot be reordered, but it’s hard for me to find documentation to prove this.

+6
source

All Articles