线程核心方法-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
     * does not lose ownership of any monitors.
     *
     * @param  millis
     *         the length of time to sleep in milliseconds
     *
     * @throws  IllegalArgumentException
     *          if the value of {@code millis} is negative
     *
     * @throws  InterruptedException
     *          if any thread has interrupted the current thread. The
     *          <i>interrupted status</i> of the current thread is
     *          cleared when this exception is thrown.
     */
    public static native void sleep(long millis) throws InterruptedException;

特点:
1、线程进入休眠,线程的状态是TIMED_WAITING

thread to sleep
TIMED_WAITING

2、线程不会释放自己的监视器的所有权,言外之意就是不会释放拥有的锁。

The thread does not lose ownership of any monitors.
Code

State

3、入参不能为负数

IllegalArgumentException – if the value of millis is negative
IllegalArgument

4、假如该线程已经进入休眠,这时候中断该线程,那么该线程的中断状态将会被复位,同时抛出异常InterruptedException

InterruptedException – if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown
Interrupt
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。