• Post Reply Bookmark Topic Watch Topic
  • New Topic

wait() and notify()!!!  RSS feed

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,
could you please help me with interpreting the followin codes as to what's going on there?

i have hard time understanding wait() and notify() here. As shown in the codes, i think when start() is invoked b starts invoking run()and the main thread go ahead runing codes right after b.start(). but we want b to sum 1 - 10 first and have main thread print that sum so we have to have main thread wait. instead, b is waiting here. i don't really understand this. i thought b is spawned and has gone to invoke run() not wait(). can you please explain me this process?
thanks.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key is that both are synchronizing on the same object, so when you call b.wait() in the main thread it goes into the b's wait pool until notify or notifyall() is called on it which is exactly what happens in the run method when the loop finishes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!