It's not a secret anymore!*
The moose likes Threads and Synchronization and the fly likes difference between wait and sleep? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "difference between wait and sleep?" Watch "difference between wait and sleep?" New topic
Author

difference between wait and sleep?

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: 320
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
SCJP - 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.

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

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
Romeo Son
Ranch Hand

Joined: Mar 12, 2007
Posts: 92

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).


SCJP 1.4, SCJD, OCE EJBD 6
babor hossain
Greenhorn

Joined: Jun 20, 2010
Posts: 1
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.
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 784

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() => Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.

void wait(long timeout) => Causes 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) => Causes 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.


Thread class have following method

JavaDocumentation wrote:static void sleep(long millis) => Causes 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) => Causes 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.





SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP 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
Sleeping thread takes locks with it?
Q on thread at Jxam
Thread
About Object lock
wait() vs sleep()