aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() and sleep() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait() and sleep()" Watch "wait() and sleep()" New topic
Author

wait() and sleep()

Ashwin Kumar
Ranch Hand

Joined: Apr 07, 2005
Posts: 78
what could be diffrent scenarios to use wait() and sleep() methods, it sounds the purpose of these methods is similar. I know wait() is from Object Class and sleep() from Thread, but the purpose sounds similar.PLease clarify me ???


Preparing SCEA..<br />SCBCD 5.0<br />SCWCD 1.4<br />SCJP 1.4
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
wait() and sleep() are quite different.

wait() causes a thread to give up its lock on the object on which wait is called. The thread then sits in a blocked/waiting state until the object being waited on receives a notify message from another thread. Then the waiting thread wakes up and attempts to continue executing, having regained the lock on the object. wait() must be called from a context that is synchronized on the object in question.

sleep() causes a thread to pause its execution for a specified time, taking all its locks with it. It will wake up after the time expires, without being notified. sleep() can be called from a synchronized or non-synchronized context.


SCJA 1.0 (98%), SCJP 1.4 (98%)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Basically, wait is used when a thread is "waiting" for a certain condition to be met, at which time it will presumably be "notified" so it can resume. (See the notify and notifyAll methods in Object.) This is often used with resources protected by a lock (synchronized code). While waiting, the thread releases its lock on those resources so that other threads can use them.

On the other hand, sleep is basically a pause in the thread's execution. Locks are not released while sleeping, although the CPU is freed up to do other things.

See the Java Tutorial on threads...
http://java.sun.com/docs/books/tutorial/essential/threads/index.html


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Usually you use wait and notify methods when at least two threads are sharing a common resource. It's typical in a consumer/producer relationship.

Imagine, for example, a thread (producer) which is pushing objects into a stack, and concurrently another thread (consumer) which is popping them out.

If the producer realizes that the stack is full you could make the producer thread to "wait" until the consumer has has popped out all the items. Likewise, if the consumer pops all the items out of the stack, then the stack is empty, and the consumer could notify the producer to stop waiting and continue to push items into the stack.

I wrote this sample code, but I do no have a Java compiler here, so I apologize in case it does not compile. I hope it helps at least for illustrative purposes.



[ April 26, 2005: Message edited by: Edwin Dalorzo ]
[ April 26, 2005: Message edited by: Edwin Dalorzo ]
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Here's a debugged version of Edwin's nifty example:

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Thanks Joe.

I appreciate it.
 
 
subject: wait() and sleep()