How Thread.sleep() works internally

后端 未结 5 1429
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 00:49

Let\'s say I have a thread T and it is holding one resource R. If I call Thread.sleep() on the current thread i.e T, will it release the resource R (to let other threads use

5条回答
  •  孤城傲影
    2020-12-15 01:21

    Javadoc says - sleep(): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers

    The Thread.sleep() method essentially interacts with the thread scheduler to put the current thread into a wait state for the required interval. The thread however does not lose ownership of any monitors.

    In order to allow interruption, the implementation may not actually use the explicit sleep function that most OS's provide.

    If the current thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of Thread class, then its interrupt status will be cleared and it will receive an InterruptedException.

提交回复
热议问题