java多线程

线程核心方法-sleep

2021-04-26  本文已影响0人  余生爱静

源码

  /**
     * 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
上一篇下一篇

猜你喜欢

热点阅读