This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question about synchronized code  RSS feed

 
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!