This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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 Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wait Method" Watch "Wait Method" New topic

Wait Method

Patrick Punty

Joined: Jul 01, 2005
Posts: 24
when I say wait(2000) so that means it will wait for 2 sec....right?
So my question is then what is the use of notifying a thread since after 2 sec it will automatically enter into the runnable state?

Or is it we use notify in case when we dont specify the time frame for the wait method.
Please confirm......

And one more querry
Do wait methods take lock along with them when they enter in the waiting state.?
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
What if you have called wait as :
Now this will make the thread wait for 200 sec or more...and now if you want to wake the thread just after 5 must have some mechanism to do it, Notify helps to do so.

For your second query you are suggested to read api of wait method in object class, you will get the answer.

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Vidya Sankar

Joined: Aug 17, 2005
Posts: 20
hi patrick,

The wait method releases the lock. Therefore for the thread that calls wait does not become runnable until it obtains the lock again. It will have to be notified by some other thread.

When a waiting thread is timed out/notified/interrupted it does not go the runnable state - but is in the waiting for lock state. A waiting thread can come out of wait before its timeout if there is a notify/notifyAll call - do read the api for wait calls.

When a thread calls the wait without specifying anytime then the only way for it to get back to waiting for lock state is either to be notified or interrupted.

Also note that notifying alone doesnot release the lock. The thread notifying has to complete its synchronized code. i.e, after notifying if the notifying thread performs some other action within the same synchronized context then the lock will not be released. Only aferwardsis the lock released by the thread that notifies other waiting threads.
[ October 09, 2005: Message edited by: sankar velamuri ]

SCJP 1.4
Patrick Punty

Joined: Jul 01, 2005
Posts: 24
Thanks sandy,

One more thing is it always necessary to call a notify method to bring a waiting thread back to runnable state, or is it only necessary with the case of wait with no time frame defined.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
To a great extent you are right patrick.
it only necessary with the case of wait with no time frame defined.
But I think that if you dont at all specify the notify then the compiler itself calls the notify on the thread after some time..(I am not sure of it).

I'll confirm it very soon, By that time if anyone else could throw some light then it would be great.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Here it is:

Read the post by Michel Ernest that will help you
Ashok George
Ranch Hand

Joined: Mar 25, 2005
Posts: 87
waw really good one!!! thanx a lot guys!!!
Sudhakar Gaharwar

Joined: Oct 10, 2005
Posts: 15
This is very simple because wait have two forms i.e.,
wait(); - for this we must have to call notify where as in the second form we needn't to call notify because it works timeslice .
wait(1000)- it will wait for 10 sec & after that it will be automatically started.

Note : we can call the notify in case of wait(time) also.
I agree. Here's the link:
subject: Wait Method
It's not a secret anymore!