• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to Synchronize a class??

 
mayank saxena
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i synchronize a whole class, instead of a method?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not do that. you can only synchronize methods or code blocks
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic