java - Difference between wait() and sleep() -


What is the difference between a wait () and sleep? > In the thread?

Do I understand that waiting () -The thread is still in running mode and uses the CPU cycle but sleep () Does any CPU cycle cure?

We have both wait () and sleep () : how does their implementation vary at a lower level?

A "awake" by another thread can be waiting on which phone is being monitored A wait (and notify ) should be in a block synchronized on the monitor object while one can not, While sleep no:

  object mon = ...; Synchronize (mon) {mon.wait (); }  

At this point the currently executed thread waits and the monitor can be released another thread may be

  synchronize (Mon) {mon.notify (); }  

(the same mon object on the same page) and the first thread (assuming the only thread waiting on the monitor) will wake up

If you are waiting for more than one thread on the monitor, you can also call it - it will wake up all of them However, only one monitor from the thread (Remember that wait is a synchronized in block) and continue - others will be blocked until they can get the monitor lock .

Another issue is that you call wait on your own (i.e. you wait on the monitor of the object) while on sleep

Yet another issue is that you can get wait from fake wakeups (i.e. that means waiting for the resume for some reason is). Always wait spinning on some condition as follows:

  synchronize {while (! Condition) {mon.wait (); }}  

Comments