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

Problem with wait() & notify()  RSS feed

 
Samanta Bes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm an Italian new Entry here!
I have a problem with wait and notify! I try to explain my problem...
I created a Singleton class!
I have 2 thread from 2 different classes. From evrey thread I get a static object from Singleton with Singleton.getObj()

First Thread:



Second Thread:



But this not work! Can you tell me why???
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Samanta. Welcome to The Ranch!

When you say it's not working...what exactly is happening? And what are you expecting to happen?
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you get an exception? Does nothing happen? Please TellTheDetails.
 
Samanta Bes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't exception!



If I put those prints on the console i see "WAIT" and never "ok"!!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's still not enough information to tell what's going on, I'm afraid.

When that thread calls wait(), it will wait until another thread calls notify() on the same object. Are you sure that the other thread is running and that it calls notify() after the first thread calls wait()? How do you start them both off?
 
Pankaj Kumarkk
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
you can refer to below for an exampleo of wait-notify and then co-relate with your code.



Thanks,
Satish
 
Rohan Dhapodkar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only case when your example will not work is, thread t2 entered first into synchronized block and it executes object.notify() ( which will lost !!!) and after that thread t1 entered into synchronized() block and now waiting for notification. This is issue with Object.wait() and notify() method, where notifications will be lost.
 
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!