What is the difference between sleep(), wait() and suspend()?
Thread.sleep() is used to suspend a thread execution for a specified time. or it sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has aquired --
i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread.
Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.
t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.
object.wait() sends the current thread into the "Not Runnable" state, like sleep(), but with a twist. Wait is called on a object, not a thread; we call this object the "lock object." Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the "wait list" associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.
more discussions here.
i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread.
Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.
t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.
object.wait() sends the current thread into the "Not Runnable" state, like sleep(), but with a twist. Wait is called on a object, not a thread; we call this object the "lock object." Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the "wait list" associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.
more discussions here.




























3 comments:
u have to elaborate it more by giving good examples
while using wait() and notify() method in Java Its important to understand that wait() should be called in a loop if multiple thread can wait on same condition because when threads gets notified there is no guarantee which thread will get lock and return from wait method. so if suppose two threads A and B are waiting on a condition and receives notification then both try to get lock , luckily A got lock and executed and made the condition false again but if thread B doesn't check the condition again after getting lock (which is why we need to check condition in loop) it will run even if it supposed to wait. So its important to recheck the condition after getting lock and wait again if condition is false again.
JP
Why wait() and notify() method must be called from synchronized context
Major difference is lock releasing, so if you are programming taking lock in account than using sleep for creating pause can result in deadlock because it doesn't release lock. See here more difference between wait, sleep and yield in java
Post a Comment