• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about synchronized code

 
Chi Tang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all:
Do I have to put wait() and notify() in the synchronized code.
I have a method like
public static synchronized void increment()
{
// I didn't put wait() and notify() here.
}
this method was still able to call by multiple thread.
The goal I want to achieve here is every time only allow only one
thread to access .
Please help.
Thank you
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chi
wait, notify, and notifyAll must always be called from within synchronized code. However, just because you have synchronized code doesn't mean you always have to use them.
they are used when you may hav multiple threads trying to access the synchronized block and you need to have some way to control when they can access the code and when they get told that it is ok for them to do so.
hope that was what you were looking for
Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!