It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait() method" Watch "wait() method" New topic
Author

wait() method

Divya Eknath
Greenhorn

Joined: Jul 27, 2006
Posts: 9
What is the use of having timeout argument for the wait() method?
If i understand correctly, both when the
(1)thread is notifiied and when the
(2)time in the wait method expires,
the thread goes to Blocked-for-lock acquisition state.And when the lock on the object is released it goes to ready-to-run state and then to running state whenever it gets chance.

So the call to wait() method returns at the same time in both cases.

And also when the thread is interrupted, does the thread go to Blocked-for-lock acquisition state-->>Ready State-->>Running annd then throws the InterruptedException.Please correct me if i am wrong.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

If you specify wait time in milli seconds then the thread will stop waiting after that time. You could say wait(1000) and make the thread to wait for 1 second and then continue execution or if the thread is notified or interrupted before 1second execution of the rest of the program will continue


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Tauri Valor
Ranch Hand

Joined: Aug 03, 2005
Posts: 166

And also when the thread is interrupted, does the thread go to Blocked-for-lock acquisition state-->>Ready State-->>Running annd then throws the InterruptedException.Please correct me if i am wrong.


When the thread is interrupted by another thread, an InterruptedException is thrown AFTER the thread regains it's lock on the object


A Moment's insight is sometimes worth a Life's experience.
 
 
subject: wait() method
 
Similar Threads
basic question on releasing a lock
threads
From Velmurugan's Notes
Thread
wait() and sleep()