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

Peter Veentjer alarmnummer at gmail.com
Thu Nov 23 15:18:33 EST 2006


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;
   }
}

In this case the release of the lock in setValue and the get of the
lock in getValue. provides safe 'piggy backing on synchronization' for
the variable value.

Removal of the synchronized blocks could change program behaviour. Is
the compiler to allowed to remove these locks? My guess would be that
this isn't allowed.


More information about the Concurrency-interest mailing list