| 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
|
|
|
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.
|
 |
 |
|
|
subject: synchronization problem: Please help me
|
|
|