I have noticed a few times that Java implements synchronized blocks thusly:
aload 4
dup
astore 5
monitorenter
[ various instructions ]
aload 5
monitorexit
Why does it duplicate the variable when running synchronized blocks?

Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Roedy Green - 13 Sep 2007 08:16 GMT
On Thu, 13 Sep 2007 01:27:03 GMT, Joshua Cranmer
<Pidgeot18@verizon.net> wrote, quoted or indirectly quoted someone who
said :
>astore 5
>monitorenter
[quoted text clipped - 3 lines]
>
>Why does it duplicate the variable when running synchronized blocks?
Byte code has almost no optimisation. Sun does all its optimisations
in HotSpot.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Barb Knox - 13 Sep 2007 09:33 GMT
> I have noticed a few times that Java implements synchronized blocks thusly:
>
[quoted text clipped - 7 lines]
>
> Why does it duplicate the variable when running synchronized blocks?
Because you might have code like this:
synchronized (something) {
...
something := somethingElse;
...
}

Signature
---------------------------
| BBB b \ Barbara at LivingHistory stop co stop uk
| B B aa rrr b |
| BBB a a r bbb | Quidquid latine dictum sit,
| B B a a r b b | altum viditur.
| BBB aa a r bbb |
-----------------------------