[concurrency-interest] lock removal by compiler together with monitor-lock order rule

Joe Bowbeer joe.bowbeer at gmail.com
Thu Nov 23 16:32:04 EST 2006


On 11/23/06, Peter Veentjer <alarmnummer at gmail.com> wrote:
> I have a question about the compiler removing 'useless' synchronizations.
>
> eg:
>
> class Foo{
>     int value;
>
>    setValue(int value){
>        this.value = value;
>        synchronized(this){}
>    }
>
>    int getValue(){
>        synchronized(this){}
>        return value;
>    }
> }
>

The (runtime) compiler is not allowed to remove synchronized blocks if
there's any chance that multiple threads will be executing them.  If,
for example, the compiler is smart enough to determine that these
methods are only called by the same thread, then the synchronized
blocks could be removed.

By the way, the "roach motel" semantics would allow your assignments
to move into the synchronized block from above or below, but not to
leave the synchronized block, or jump over it.


More information about the Concurrency-interest mailing list