There is a lot of material on stack-overflow about synchronization, but I still haven't acquired quality content about deciding which object to use as an intrinsic lock. Can some one actually make a good answer as a rule of thumb?
So should I choose 'monitor' as an instance variable or local variable or instance owning the method? All three of them do the job well. Also primitive value wrapper classes use 'pools' so no problem there as well, as threads 'attack' the same lock.
So why is it better to do this (this):
class A {
void methodA(){
synchronized (this){
//some code
}
}
}
over this(instance variable):
class A {
String monitor = "monitor";
void methodA(){
synchronized (monitor){
//some code
}
}
}
or over this(local variable):
class A {
void methodA(){
String monitor = "monitor";
synchronized (monitor){
//some code
}
}
}
They all work fine/same. So why did I read that I should avoid local variables when they implicitly use pools to store objects? What matter does the scope of variables make in this case?
Thanks!