What is difference between wait
and sleep
?
wait() – when synchronized multiple threads access same Object one by one. sleep() – when synchronized multiple threads wait for sleep over of sleeping thread.
Sleep relinquishes the CPU for specified amount , so that other process can execute. While Delay is just busy waiting for the amount to expire and then continue once expires, it do not execute any other process during this period.
wait() is a non-static method of Object class. sleep() is a static method of Thread class. Waiting threads can be woken up by other threads by calling notify() or notifyAll() methods. Sleeping threads can not be woken up by other threads.
The major difference between yield and sleep in Java is that yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute.
wait
waits for a process to finish; sleep
sleeps for a certain amount of seconds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With