Sei sulla pagina 1di 4

6/14/13

difference between wait and sleep? (Threads forum at JavaRanch)

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Java Threads and Synchronization

Author
purushotham singam Greenhorn Joined: Aug 23, 2007 Posts: 8

difference between wait and sleep?


posted 8/26/2007 12:23 PM

hi all, can any one explain d/b sleep and wait method

Bob Ruth Ranch Hand Joined: Jun 04, 2007 Posts: 318

posted 8/26/2007 10:43 PM

Sleep, suspends the current thread for a period of time. The expiration of that time is what will make the thread runnable. Wait, suspends the current thread until some event, like the freeing of a lock. When someone else calls a notify it will make this thread runnable again.

-----------------------Bob SC JP - 86% - June 11, 2009 paritosh ranjan Ranch Hand Joined: Aug 24, 2007 Posts: 62

posted 8/27/2007 12:40 PM

wait must be called from that part of code which aquires the lock otherwise it will throw an exception.
1/4

www.coderanch.com/t/233929/threads/java/difference-wait-sleep

6/14/13

difference between wait and sleep? (Threads forum at JavaRanch)

this is not the case with sleep,you can call sleep from anywhere,it will just make the calling thread sleep for the specified time.sleep is a static method.

Saurabh Patil Ranch Hand Joined: Dec 11, 2005 Posts: 45


I like...

posted 8/29/2007 11:21 AM

Originally posted by paritosh ranjan@bitm: [QB]wait must be called from that part of code which aquires the lock otherwise it will throw an exception. QB]

I think notify() should be called from the piece of code which has the lock and wait() should be called from piece of code which wants to acquire the lock.

All for one and one for all! paritosh ranjan Ranch Hand Joined: Aug 24, 2007 Posts: 62

posted 8/29/2007 12:42 PM

Yes What I wanted to say was that both wait and notify should be called from a synchronized block

Romeo Son Ranch Hand Joined: Mar 12, 2007 Posts: 92


I like...

posted 8/30/2007 4:56 PM

Another main difference between wait and sleep is that in calling wait the current executing thread releases the ownership of the monitor while in calling Thread.sleep the current thread that sleeps does not lose ownership of any monitors (if any).

SC JP 1.4, SC JD, OC E EJBD 6

babor hossain Greenhorn Joined: Jun 20, 2010 Posts: 1

posted 6/20/2010 12:31:36 PM

As we know java programming language supports multi threading concept. The threads are running at the same time if you want to stop execution of a thread and give the control to another thread these two functions, are supported by java for the purpose Wait() : This function takes parameters as milliseconds and puts the thread in wait state for the desired time of the programmer after time passes the execution starts again. Sleep() : This function is also used for same purpose using his function by java you can put a thread in sleep state .sleep does not contains any parameters so the thread will not be automatically start execution It needs a wake up signal

www.coderanch.com/t/233929/threads/java/difference-wait-sleep

2/4

6/14/13

difference between wait and sleep? (Threads forum at JavaRanch)

again which can be Notify().or other function are also provided by java. So the main difference in Wait() and sleep() is wait takes time parameter and wait for specific time only and sleep throws a thread in sleep mode for unspecified time.

Ninad Kulkarni Ranch Hand Joined: Aug 31, 2007 Posts: 775
I like...

posted 6/20/2010 2:39:45 PM


babor hossain wrote:

As we know java programming language supports multi threading concept. The threads are running at the same time if you want to stop execution of a thread and give the control to another thread these two functions, are supported by java for the purpose Wait() : This function takes parameters as milliseconds and puts the thread in wait state for the desired time of the programmer after time passes the execution starts again. Sleep() : This function is also used for same purpose using his function by java you can put a thread in sleep state .sleep does not contains any parameters so the thread will not be automatically start execution It needs a wake up signal again which can be Notify().or other function are also provided by java. So the main difference in Wait() and sleep() is wait takes time parameter and wait for specific time only and sleep throws a thread in sleep mode for unspecified time.

@ babor hossain Welcome to JavaRanch Object class have following methods

JavaDocumentation wrote:

void wait() => C auses the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. void wait(long timeout) => C auses the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. void wait(long timeout, int nanos) => C auses the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed. void notify() => Wakes up a single thread that is waiting on this object's monitor. void notifyAll() => Wakes up all threads that are waiting on this object's monitor.
www.coderanch.com/t/233929/threads/java/difference-wait-sleep 3/4

6/14/13

difference between wait and sleep? (Threads forum at JavaRanch)

Thread class have following method

JavaDocumentation wrote:

static void sleep(long millis) => C auses the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. static void sleep(long millis, int nanos) => C auses the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers.

SC JP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SC JP FAQ - SC JP Mock Tests Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions

I agree. Here's the link: http://aspose.com/file-tools

subject: difference between wait and sleep?

Similar Threads About Object lock Sleeping thread takes locks with it? Q on thread at Jxam wait() vs sleep() Thread
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 14, 2013 05:06:29 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/233929/threads/java/difference-wait-sleep

4/4

Potrebbero piacerti anche