• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

wait( ) method confusing a lot ...

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thread.wait();

I got this from K&B method , every object has list of thread waiting for lock for that object and one thread has lock . now suppose this thread calling wait() method , then it will go inside the list & wait for lock .

Is everything is right till this point ???

Now my doubts are :
1] suppose there are other threads present in that list , so will anyone get the lock() just after releasing lock or is there any method required for that ( may be notify() method ) ???

2] We are just creating a thread object & calling wait() method , so in this case there is no object , no lock , no waiting list .. so where this thread will go after calling wait() method ???

please help me ..
I am really confuse a lot ..

thanks a lot .
 
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
my second doubt is clear now , we have to call wait() method from synchonized context only ( infact I gave the reason for this ) so in any way before calling wait() method we will have an object , so we will have lock , we will have waiting list & in this this thread will wait ..

Is explanation is right ??

please solve my first doubt ??

thanks .
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
Now my doubts are :
1] suppose there are other threads present in that list , so will anyone get the lock() just after releasing lock or is there any method required for that ( may be notify() method ) ???

2] We are just creating a thread object & calling wait() method , so in this case there is no object , no lock , no waiting list .. so where this thread will go after calling wait() method ???



1. The wait() method waits for notifications -- it just so happens that it needs to re-acquire the lock that it gave up upon waiting before it can return. A call to notify needs to be done to send the notification.

2. Waiting without holding the lock will generate an exception.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic