We have a class A which has a synchronized method say abc(). Two instances a1 and a2 are created from A.
Now two threads t1 and t2 call the abc() method on a1 and a2 respectively. The question is:
Will the execution of abc() be synchronized?
My answer was no. This is because the scope of abc() is the object and not the class. Hence when the same method is called on different instances, their scope is restricted to that instance and execution continues. Had the threads been executing the method on the same object, abc() would have been synchronous.
The solution to this is to declare the method as static. This keeps the scope of the method at the class level and would behave synchronously across multiple instances.
Posted from Blogium for iPhone
No comments:
Post a Comment