[concurrency-interest] why bytecode difference between synchronized block and method

Thomas Hawtin tackline at tackline.plus.com
Sun Nov 19 09:21:46 EST 2006

Peter Veentjer wrote:
> Why is there a difference in the bytecode between a synchronized block
> (as first statement of a method) and a synchronized method? I don't
> understand why there should be any difference but if I look at the
> bytecode of the following example, there is.

For compactness, there is a special representation for synchronized 
methods (it's just a bit, together with abstract, public, protected, 
private, etc. flags). If you write it long hand, so does javac: 
monitor-enter(this); try { ... } catch (* exc) { monitor-exit(this); 
throw exc; } monitor-exit(this);.

By the time it is has been compiled into machine code, there should be 
no discernible difference.

Tom Hawtin

More information about the Concurrency-interest mailing list