JavaMemoryModel: FWD: Question regarding nested synchronized blocks

From: Bill Pugh (
Date: Mon Apr 22 2002 - 19:22:32 EDT

[ Forwarded from "Jack, Paul" <>; who isn't subscribed
via that email address]


My apologies if this question isn't relevant to
the list, but I have a question regarding the
existing Java Memory Model spec (chapter 17
of the Java Language Spec).

I read in a JavaWorld article that it's forbidden
for two synchronized blocks to be merged:

     sychronized (lock) {
     synchronized (lock) {

Now assuming there's no side-effects, an optimizing
compiler can legally reorder <op1> and <op2>, or
reorder <op3> and <op4>, but it can't, for instance,
put <op3> between <op1> and <op2>. This all makes
sense to me.

My question involves nested sychronized blocks.
According to the JLS (17.9), "Any association between
locks and variables is purely conventional. Locking
*any* lock conceptually flushes *all* variables from a
thread's working memory..."

So given nested locks...

     synchronized (lock1) {
         synchronized (lock2) {
         synchronized (lock2) {

...what reorderings are allowed? It seems that
<op2><op1> <op5><op4><op3> <op7><op6> would
be legal, but not <op4> <op2><op1> <op5>
<op7><op6> <op3>. Am I correct in this assumption,
and if I am, what's the rationale?

Thanks for your time...

JavaMemoryModel mailing list -

This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:39 EDT