This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes synchronization problem: Please help me Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "synchronization problem: Please help me" Watch "synchronization problem: Please help me" New topic
Author

synchronization problem: Please help me

Senthil Kumaresan
Greenhorn

Joined: May 29, 2003
Posts: 17
Friends,
I was given the following class (IncrementImpl) already written. Now we have so many threads which access this concurrently. How can I make it synchronize it.
Please help me.

Thanks,
Senthil.
-------------------
public class IncrementImpl {
private static int counter = 0;
public synchronized void increment() {
counter++;
}
public int getCounter() {
return counter;
}
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

Are you expecting something more complicated than making the getCounter() method synchronized?


[Jess in Action][AskingGoodQuestions]
Senthil Kumaresan
Greenhorn

Joined: May 29, 2003
Posts: 17
Ernest Friedman,
Thanks for your response. I think synchronizing getCounter method is enough.
Thanks a lot.
Senthil.
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
I suppose it's a little too academic, but declaring counter as volatile is an alternative solution, as long as you getCounter() method stays as atomic as it is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronization problem: Please help me
 
Similar Threads
Question on static
how to generate field dynamically in wicket
I want to demonstrate an unsafe thread
Java Problem = List of errors, Clueless. HELP
Are context-parameters thread-safe?