Why is it better to wait for a signal than to just use sleep

Why is it better to wait for a signal than to just use sleep()?

Solution

A sleep() method will consistently keep checking whether the sleep timer is finished so that it can wakeup, and start processing whereas, the wait() method will just keep idle till it receives a signal.

A wait() method will be woke up by some external method, whereas, the sleep() wakesup on its own.

Speaking in simple words, a sleep() timer is something like, a person sleeping who decides to wakeup on his own by some time. If this is the case, he has to keep on waking up at regular intervals and should check whether his time to wakeup has arrived.

On the other hand, a wait() is something like, asking somebody else who is awake to wake him up after some time, or some activity. In this case, he will have a peaceful sleep, and will be wokeup by somebody else.

Talking the similar way, a wait() method will go into blocked state, and should be pulled out of it by some other process which is in running state. Whereas, the sleep() method will keep consuming some resources to check whether the time is ran out.

Why is it better to wait for a signal than to just use sleep()?SolutionA sleep() method will consistently keep checking whether the sleep timer is finished so t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site