File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

difference between wait and sleep?

 
purushotham singam
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

can any one explain d/b sleep and wait method
 
Bob Ruth
Ranch Hand
Posts: 320
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
paritosh ranjan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
paritosh ranjan
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes

What I wanted to say was that both wait and notify should be called from a synchronized block
 
Romeo Son
Ranch Hand
Posts: 92
Android Eclipse IDE Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
babor hossain
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
Hibernate Linux Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic