The difference of sleep and wait(sleep与wait的区别)
- sleep is the method of thread,but the wait is the method of object.(sleep是线程的方法,wait是对象的方法)
- sleep will not release hte object monitor(Lock),but he wait will be release the monitor and add to the Object monitor waiting queue.(sleep不会释放监视器的锁,wait会释放监视器的锁。也就是说,sleep是将线程block,整个代码块都会block,其他线程也无法执行该代码逻辑,但是wait是将线程block后,会释放lock,并且放在wait队列,不会影响其他的线程的执行synchronized同步代码块.并且还会将监视器对象添加到wait队列)
- use sleep not depend on the monitor,but wait need.(sleep不需要依赖synchronized,但是wait需要.)
- The sleep method not need to wakeup,but waite need.(wait(10))(sleep不需要主动唤醒,wait需要(wait(10)除外))