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.
Joined: May 13, 2010
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
Joined: Sep 20, 2010
This is because the synchronized keyword is an implementation detail, so Javadoc doesn't report its use.