| Author |
How to Synchronize a class??
|
mayank saxena
Greenhorn
Joined: Mar 26, 2007
Posts: 1
|
|
|
How can i synchronize a whole class, instead of a method?
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
|
You can not do that. you can only synchronize methods or code blocks
|
apigee, a better way to API!
|
 |
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
|
|
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 Bhattacharjee
LinkedIn - Blog
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
|
Does it mean the class is synchronized or the method is synchronized on the class. I hope you get the nuance in the two statements.
|
 |
Pratik Patel
Greenhorn
Joined: Mar 20, 2007
Posts: 9
|
|
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.
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
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.
|
 |
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
|
|
Originally posted by Nitesh Kant: What does synchronizing a class mean?
I think the following sentence is incorrect. Syncronize the class ! Where as the following is meaningful to me. Syncronize on the Class lock , like syncronize on the object lock. So I go wiht #2.
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
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
|
 |
Rahul Bhattacharjee
Ranch Hand
Joined: Nov 29, 2005
Posts: 2300
|
|
No matter how many days the discussion extends , but the bottom line is that it should be of some help to someone. Have a great day !
|
 |
 |
|
|
subject: How to Synchronize a class??
|
|
|