Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

simple wait & notify  RSS feed

 
george georgeme
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand the usage of notify(). In the following code, there is a syncronized boolean SectionFree set originally to true.
the following section belongs to a syncronized void method called inside run().
when there is only one thread running the code works ok
but when mutpiple threads run, they don't get "notified". They stay 4 ever at the wait()....
Obviously I am missing something here..... P.S. there are no error outputs..

THANKS..

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi George. Welcome to The Ranch!

One question that springs to mind, that we can't tell from that code: are all the threads synchronizing and calling wait/notifyAll on the same object?
 
george georgeme
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matthew ,
thanksss....

well, my problem is that I don't really understand the question :-( , I mean, I simply don't know if yes...

I didn't want to bother everybody with the all code... but now I realize that my problem is elsewhere.
As you can see commented within the code, there is a critical section where there should be one thread at a time... And I obviously can't do that...




 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, here's what's happening. All your synchronization is happening on the MyServer object in question. Your waitNextUser method is synchronized on it (that's what the keyword does when applied to a method), and that's the object that wait() and notifyAll() is being called on. That means that the separate instances of MyServer aren't communicating with each other - they're all just running as normal.

If you just want to stop multiple threads running the same section of code at the same time, there's no need to use wait/notify. All you need is to use a synchronized block, making sure they are using the same object. For instance, the class itself, or a static member variable:

It looks to me that will do what you need in this case. The wait/notify comes into play in more complex scenarios, if you need to temporarily release the synchronization lock because you're also waiting for some other condition to be true (and you want to avoid blocking all the other threads). The Java tutorials have an example of this at http://download.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html.

 
george georgeme
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, I get it...
It was the "super" object I need to "control"...
I will go through the site suggested, it looks exactly what I need

Thanks alot everybody...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!