aspose file tools*
The moose likes Threads and Synchronization and the fly likes How to synchronize all methods of a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to synchronize all methods of a class" Watch "How to synchronize all methods of a class" New topic
Author

How to synchronize all methods of a class

Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
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


Thanks
user101
Problems And Solutions - Algorithms
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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

Joined: May 13, 2010
Posts: 104
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

This is because the synchronized keyword is an implementation detail, so Javadoc doesn't report its use.
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
Ok.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to synchronize all methods of a class