• 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

wait( ) , notify( ) , notifyAll( ) methods ...

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this right about notify() method ...

when a thread is in a waiting queue ( after calling wait() method ) , notify() method is for waking up this thread .

But I am confuse in this :

who will call notify() method ?
how this thread will know that some body has called notify() method ?
how notifyAll() work ?
If possible can any body explain me all three method ?

thanks a lot .
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when a thread is in a waiting queue ( after calling wait() method ) , notify() method is for waking up this thread .

The notify method "tells" the other thread that the work is done. Since it happens, the tread which called wait() is ready to go back in execution. However, it doesn't mean that it will actually go into execution right after that, it will depend on the scheduler.

who will call notify() method ?
Suppose you write a code like this.

The public void run() method of your thread (the one i called MyThread) will probably call the notify() method after it has done the necessary job.
Once MyThread calls notify and leave the synchronized block, TestThread class, which has a reference to MyThread will be able to get back to work.

how this thread will know that some body has called notify() method ?
As i said earlier, the caller has a reference to the thread that called notify() method.

how notifyAll() work ?
This is important. Suppose you have more than one references to MyThread and all of them have called the wait() method.
When MyThread calls notify() method only one of the callers will be awaked. It's impossible to know for sure which one will be. To solve this problem there is the notifyAll() method, which will notify all references to MyThread that it's done with the job.

If possible can any body explain me all three method ?
Hope i could help you.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Leandro ,
But I am still confuse ...

Can you tell me one bye one in simple words ...

when you called wait method in your code ...


1] Who will wait , the thread who is invoking wait() method ( my ) or the thread which is executing this code ( may be main thread ) ....

2] you have not called this from synchronized block ...

3] who will notify & why & how ....

please answers my questions .. it would be great help for me ...

thanks a lot .
 
reply
    Bookmark Topic Watch Topic
  • New Topic