• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to synchronize all methods of a class

 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i want to synchronized all the method inside my class.how to do that.

Also is there any other method to synchronized single method other than using synchronized keyword

Thanks
user101

Problems And Solutions
 
Stephan van Hulst
Bartender
Pie
Posts: 5608
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satyajit,

If you want to synchronize all the methods in your class, you will have to use the keyword synchronized on every method. Carefully decide whether it's necessary to synchronize a method though. Some don't need it, and synchronizing them will add an unnecessary performance penalty.

You can also synchronize your code by making your fields volatile. This however, will not stop race conditions from occurring, and you will have to add a kind of mutex to your code. You will have to use the wait() and notiftyAll() methods to carefully direct which parts of your code can be accessed by which threads. This is a very painful process, and this is why it's not wise to replace the synchronized keyword.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
offtopic.regarding the extra post. i could not see any option to delete the post.Sorry for that.

Thank You for explanation.

We say vector is thread safe.So how vector class was written
from your explanation what i see is each method of vector class must be synchronized but when i went through signature in javadocs i did not find synchronized keyword on method signature.
 
Stephan van Hulst
Bartender
Pie
Posts: 5608
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because the synchronized keyword is an implementation detail, so Javadoc doesn't report its use.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic