Optimizations on synchronized methods in java/C++

Srikanth Raju <srikiraju@gmail.com>
Tue, 20 Jul 2010 01:35:31 -0700 (PDT)

          From comp.compilers

Related articles
Optimizations on synchronized methods in java/C++ srikiraju@gmail.com (Srikanth Raju) (2010-07-20)
Re: Optimizations on synchronized methods in java/C++ marcov@turtle.stack.nl (Marco van de Voort) (2010-07-21)
Re: Optimizations on synchronized methods in java/C++ Pidgeot18@verizon.invalid (Joshua Cranmer) (2010-07-21)
| List of all articles for this month |
From: Srikanth Raju <srikiraju@gmail.com>
Newsgroups: comp.compilers
Date: Tue, 20 Jul 2010 01:35:31 -0700 (PDT)
Organization: Compilers Central
Keywords: Java, question, optimize
Posted-Date: 21 Jul 2010 02:58:47 EDT

I was wondering whether any optimizations are automatically done by
the compiler or VM in Java in classes where methods are defined as
synchronized. I'm working with a library where a typical function is
defined so,


class Foo
{
private int a = 0;
private int b = 0;
public synchronized doSomethingWithA( int var )
{
        int tmp = some_function( var ); // heavy computation
        a += tmp;
}
public synchronized doSomethingWithB( int var )
{
        int tmp = some_function( var ); // heavy computation
        b += tmp;
}
};


Can the compiler see that the first statement in the methods are local
and therefore not lock the object for that statement, and hold the
lock only for the actual addition part, so that it saves time?



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.