sleep() vs. wait()
CALLING LOCATION
wait() must be called from synchronized method / block
sleep() can be called from anywhere
TARGET
wait() is instance specific method called on object (exists in Object class)
sleep() is static method called on thread (exists in Thread class)
LOCKING
wait() releases the acquired lock when thread is waiting
Thread.sleep() keeps the lock even if thread is waiting
WAKE UP CONDITION
After wait() , thread will wake up when notify() or notifyAll() is called
After sleep() , thread will wake up when at least the time expires or call interrupt()
USAGE
sleep() is used to pause a Thread execution
wait() is used for Inter-thread communication
No comments:
Post a Comment
Note: only a member of this blog may post a comment.