• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to synchronize all methods of a class

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok.

Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic