• 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()?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this doubt about them.
Say if I am doing any code which is synchronized , I'll use wait() so that no other thread exceutes this code while I am executing. And once I finish doing the synchronized block/code/method I'll say notify () or notifyAll().
I am right in understanding these methods.
Pls. can anybody clear my doubt.
thanks
sudha
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sudha,
The concept is this way:
Every object in java can act as what we call a monitor. A monitor is basically an object that has some sychronised code. It is a shared resource we can say.
Every monitor, has a lock associated with it. If a thread wants to excute the synchronised code of the monitor, it needs to get the lock on the monitor.Once a thread locks a monitor, no other thread can access it.
Now, when a thread gains control over a monitor and excutes
wait(), then the current thread gives up the lock on the monitor and goes into a pool of waiting thread.(Threads waitig to acquire lock on the monitor).The monitor is now available to any other thread that is seeking the monitor.
Now when some other thread excutes the notify() or notifyAll()method inside the monitor, then one or more of the threads from the waiting pool for that monitor will move into a seeking Lock state for the monitor. The thread that acquires the lock can excute the monitor.
One advantage of this approach is that a thread need not have to check the state of the monitor(ie lock or unlock)on its own. Its will be notified by the java system.
Hope my explanation helps.
 
Sudha Kris
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation Rajesh.
Apart from R&H which is a good book or site to get all funda about Thread clearly.
Practical Application of threads? Animation, I suppose.
Most important sample questions on Threads
Thanks
Sudha
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic