[concurrency-interest] Synchronizing on methods than on code blocks is faster

Aleksey Shipilev aleksey.shipilev at oracle.com
Mon Sep 28 06:11:19 EDT 2015


On 09/28/2015 12:26 PM, Ionut wrote:
> I am trying to understand why "*Synchronizing on methods rather than 
> on code blocks is slightly faster*" (source here 
> https://weblogs.java.net/blog/johnsmart/archive/2008/03/using_hudson_en.html
> ).

This link seems irrelevant, no source code there.

> /public synchronized void testSynchMethod() {/ /   x++;/ /}/
> 
> VS
> 
> /public void testSynchBlock() {/ /    synchronized (this) {/ /
> x++;/ /    }/ /}/
> 
> For this I did a trivial  JMH test and proved it.
> 
> Could somebody please explain to me why is this happening? May be
> the explanation stays in the assembly code generated ...

Well? Where is that "trivial JMH test", and where is the proof with the
generated machine code?

Thanks,
-Aleksey

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20150928/38863859/attachment.bin>


More information about the Concurrency-interest mailing list