At the simplest, yes, simply make the methods synchronised as that will cause them to sync on the instance of the class.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
1. Although the option 2 is better from performance point of view as YOU decide exactly which statements within the method are synch'd.
2. Even the statements that access the data from underlying storage need to be synch'd.
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
Well, if you don't declare the method synched, its not synched. But, if you meant, you can control which portions of the method are synched, yes. ;-)
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Well, if you don't declare the method synched, its not synched. But, if you meant, you can control which portions of the method are synched, yes. ;-)
I also didn't completely get your comment (like you didn't get mine :)).
What I meant was do NOT declare the method synch'd. Inside the implementation (of the method) put synch for required statements "manually" (somethihg like what he posted in the original question).
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
Well, you had said that doing the sync inside the method will allow the programmer to control which methods are synched. Well, you can do that with the synchronized keyword as well. Simply do not declare the methods you do not want to be synched as synchronized. Now, if you want to be able to control, for example, that only the excution of two lines out of a hundred line method are synched, then, yes, do the synchronization inside the method. But, if you are going to synch on the current instance, and the entire (or nearly the entire) method's content will be synched, then there is no reason not to use the synchnronized keyword on the method.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
Well, you had said that doing the sync inside the method will allow the programmer to control which methods are synched. Well, you can do that with the synchronized keyword as well. Simply do not declare the methods you do not want to be synched as synchronized. Now, if you want to be able to control, for example, that only the excution of two lines out of a hundred line method are synched, then, yes, do the synchronization inside the method. But, if you are going to synch on the current instance, and the entire (or nearly the entire) method's content will be synched, then there is no reason not to use the synchnronized keyword on the method.
We're on the same page. Perhaps you read my post too quickly. I said "whichstatements within the method".
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75