You can do it.You can syncronize methods of a class (with multiple instances / Object) by capturing the lock of Class rather than lock of object.
You can capture the lock of a class by using a static syncronized method. As the method is static is static , the JVM would try to grab the lock of the class ,so if any other thread tried to call any other static syncronized method of the same class (may be on different object of the class) then the lock of the class would be denied and hence this thread has to wait untill it gets the class lock after the first thread releases that.
[ March 26, 2007: Message edited by: Rahul Bhattacharjee ]
Rahul is correct. Obtaining a lock on the class's static method would give you the lock on the class itself. Obtaining a lock on the member method of a class would give you the lock on that perticular object.
Originally posted by Isolated Saggi: Rahul is correct.
I know he is correct! Whether it is the correct answer to the question asked or not is the actual question What does synchronizing a class mean? 1) Does it mean that using a keyword like "synchronize" all the code in the class will be synchronized? OR 2) It means that two threads have a synchronized access to a part of the class code(static method as rahul said) If the question was 1) (which i thought it was) then i think the answer is no and if it is 2) then what rahul said is absolutely correct.
Exactly rahul, this topic itself is wrong It should be How to obtain a class level lock or how to synchronize on a class!!! Thats what is the magic of english language ... a two letter word can lead to a 2 day discussion