• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Thread wait()

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does it say that wait() and notify() are to be called from a synchronized context? Whats the implication of it?

Can anyone explain please.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait,notify came into existance due to old Thread methods
suspend, stop,resume.

These methods have failed to manage locks obtained.
causing deadlocks.

To work with or manage locks wait and notify are introduced.

wait() -> releases the obtained lock and waits to get notified.
notify() -> notifies another thread(s) waiting on that object.


We can't handle the locks out side the synchronized() block. we can't creep into monitor state.

Thats wait,notify placed in Object sothat we can effectively manage the locks obtained on any Object.
[ August 11, 2007: Message edited by: Srinivasan thoyyeti ]
 
jibs parap
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to make it clearer:


In the above programme I've commented out the synchronised block around wait() and notify()[I know it will give a run time exception] just for a hypotethic scenario; I don't seem to see the importance of lock in calling wait and notify; i.e. even without owning or releasing the lock, the waiting / notifying behaviour should happen.
I know there is a flawin my thought process, please explain.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jibs,

I will try it with a more complex example, to show, why it is useful to remind the programmer to use synchronisation. The following code contains a problem (Besides that it doesn't use synchronisation for wait and notify). Can you find it?
[ August 11, 2007: Message edited by: Manfred Klug ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred Klug ,

Do you think wait,notify can be called out-side synchronized context ???
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Srinivasan thoyyeti:
Do you think wait,notify can be called out-side synchronized context

If you read the text, you will see, that I know that this will not work. It is only for demonstration purposes.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry for that
 
Arthur, where are your pants? Check under this tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic