Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
Author
purushotham singam Greenhorn Joined: Aug 23, 2007 Posts: 8
hi all, can any one explain d/b sleep and wait method
Bob Ruth Ranch Hand Joined: Jun 04, 2007 Posts: 318
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
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
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.
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
Yes What I wanted to say was that both wait and notify should be called from a synchronized block
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).
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
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...
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.
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
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
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 .
www.coderanch.com/t/233929/threads/java/difference-wait-sleep
4/4