• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with wait() & notify()

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic