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

Ionut ionutb83 at yahoo.com
Mon Sep 28 09:40:27 EDT 2015


Hi,
   Please find the sample test attached. I have launched the test with -XX:BiasedLockingStartupDelay=0
The output I got is:testSynchMethod() - 358 ops / µsectestSynchBlock() - 345 ops / µsec
This is a slightly better throughput in case of testSynchMethod(). My assumption is that probably native code generated makes this difference, but I am not sure, that's why I am posting the question here.
Please correct me if I my test is wrong or I a miss something.
Regards Ionut


     On Monday, September 28, 2015 1:11 PM, Aleksey Shipilev <aleksey.shipilev at oracle.com> wrote:
   

 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 --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20150928/47629175/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SynchronizedMethodVsBlockMain.java
Type: application/octet-stream
Size: 2100 bytes
Desc: not available
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20150928/47629175/attachment.obj>


More information about the Concurrency-interest mailing list