This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait( ) method confusing a lot ... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "wait( ) method confusing a lot ..." Watch "wait( ) method confusing a lot ..." New topic
Author

wait( ) method confusing a lot ...

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3829
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

Joined: Oct 11, 2004
Posts: 3829
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 .
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16815
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait( ) method confusing a lot ...
 
Similar Threads
Interview q
Threads Notes for Exams
Thread states, notify, notifyAll, wait
Help!
Thread doubt