• 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

Some confusion between Wait() and Notify() behaviour in java

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can anybody explain me wait and notify with real scenario example in java . I read many example on the internet but still not satisfied.
Thanks in advance.

Points which is not cleared.
How notify and wait are called on the same object?
Example: if there is 2 thread e.g t1 and t2
t1 going into waiting state because t2 want to use the same monitor . It leave the monitor.
After that , t2 has done its work and notify to t1 that you can use that monitor.
Then t2.notify() is informing t1 about the monitor that it is free.


If i give incorrect explantation .Please correct me .

Thanks.


 
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you follow the tutorial ? What did you learn ?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhawna Gupta wrote:Can anybody explain me wait and notify with real scenario example in java . I read many example on the internet but still not satisfied.


Hi Bhawna,

Is there any particular reason you need this? I've used Java for 12 years and, as yet, can still count the number of times I've needed this level of granularity on the fingers of one hand.

That's not to say it isn't worth knowing, but it would be nice to know if you have a particular scenario in mind, or whether it's just "intellectual curiosity".
Personally, I find I have to look it up again every time I use it anyway.

Winston
 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bhawna Gupta wrote:

How notify and wait are called on the same object?
Example: if there is 2 thread e.g t1 and t2
t1 going into waiting state because t2 want to use the same monitor . It leave the monitor.
After that , t2 has done its work and notify to t1 that you can use that monitor.
Then t2.notify() is informing t1 about the monitor that it is free.


If i give incorrect explantation .Please correct me .





Let us put it this way.

Example: if there is 2 thread e.g t1 and t2
t1 going into waiting state because t2 want to use the same monitor it requires something to happen and that something has not happened. It leave the monitor and goes to the waiting stage. So now the monitor is free. The CPU grants it to another thread. Let us say that another thread t2 was also waiting on the same monitor. Now t2 has this monitor and let us say that it got the time slice also. t2 starts executing. Now the time slicing happens and a third thread t3 comes in and the CPU is assigned to this new thread t3. t3 doesn't require the monitor. It completes. Now a 4th thread t4 comes in and the CPU is assigned to t4 but t4 requires the monitor. So t4 wastes the processor time in waiting for the monitor. Now processor is assigned to t2 again. And t2 is running now. t2 completes and notifies on the monitor. The CPU sends the notification to one of the waiting threads. Now t1 and t4 are both waiting. But CPU chooses one of those threads, say t4. t4 now comes out of the wait queue. So t4 gets the monitor. Once it gets the CPU also, it will start executing. Generally the programmer would make sure that it checks that the event it was waiting for has happened or not. If it has not happened, it would probably send out a notification and go back to waiting stage. Now t1 can acquire the lock and check if it the event that it was waiting for has happened or not... and this goes on..

After that , t2 has done its work and notify to t1 that you can use that monitor.
Then t2.notify() is informing t1 about the monitor that it is free.
( No a thread has no means to notify a specific thread. Notification means the processor releases a thread from the wait queue. )
 
Bhawna Gupta
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

Thanks Heena , Winston and Roger Sterling.

Actually , my concept was not clear.Due to which I faced the problem to understand the concept of wait and notify.

Thanks for your help.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic