synchronize instance method call in a static method
posted 6 years ago
I want to know if we declare an instance object in a static method and call a synchronize method of that object; when multiple threads accessing tat static method will that synchronization works fine? or not.
If I understand you right each static method call will create a new (its own copy of the instance object ie its not static just local) so the result will be no synchronization as each call is creation of a new sync object, hence new lock each time (which interestingly can be optimised away). Want to post a code snippet that would make it obvious ? Sounds like you want to use a static field on the class or the class definition instance itself to sync on.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5