Shubh, please do not cross-post, -- we consider it a criminal offense here at JavaRanch.
The synchronized static methods are handled the same as the synchronized instance methods, conceptually. However, in case of the synchronized instance method, the
this object serves as a lock, while in the case of synchronized static method, the
java.lang.Class object that refers to the class serves as a lock.
Since the synchronized instance and static methods synchronize on different objects, they can run simultaneusly (unlike the synchronized instance methods). For example:
Output:
in instanceMethod
in classMethod
leaving instanceMethod
leaving classMethod
So, can a static synchronized method run at the same time with the instance synchronized method? -- Yes, because they lock on different objects.
Can two static synchronized methods or two instance synchronized methods run at the same time? -- No, because they lock on the same object.
Does it mean that a static synchronized method and an instance synchronized method may corrupt the object state when running simulteneusly? -- Absolutely, so think it through.
[ May 07, 2003: Message edited by: Eugene Kononov ]