Thread#sleep()
/** |
1)是否释放锁
从注释中可以看出sleep的线程不会释放锁。The thread does not lose ownership of any monitors.
2)是否对中断敏感
注释中可以看出响应中断。if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
3)是否释放CPU
代码示例:
public class SleepReleaseCPU { |
运行中,CPU压力没有明显提升,说明sleep的线程会释放CPU,线程处于TIMED_WAITING
状态,进入等待队列。
Object#wait()
/** |
1)是否释放锁
是,The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method.
2)是否对中断敏感
是,if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.
3)是否释放CPU
是,wait的线程让出CPU时间片,进入等待队列。
资料:https://www.bilibili.com/video/BV1Hw411p7Qk/ 强推 河北王校长