3. 线程的休眠

目录
  1. 为何需要Thread.sleep()
  2. 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;
    
        /**
         * Causes the currently executing thread to sleep (temporarily cease
         * execution) for the specified number of milliseconds plus the specified
         * number of nanoseconds, 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
         *
         * @param  nanos
         *         {@code 0-999999} additional nanoseconds to sleep
         *
         * @throws  IllegalArgumentException
         *          if the value of {@code millis} is negative, or the value of
         *          {@code nanos} is not in the range {@code 0-999999}
         *
         * @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 void sleep(long millis, int nanos)
        throws InterruptedException {
            if (millis < 0) {
                throw new IllegalArgumentException("timeout value is negative");
            }
    
            if (nanos < 0 || nanos > 999999) {
                throw new IllegalArgumentException(
                                    "nanosecond timeout value out of range");
            }
    
            if (nanos >= 500000 || (nanos != 0 && millis == 0)) {
                millis++;
            }
    
            sleep(millis);
        }
    
  • 调用sleep休眠,线程进入TIME_WAITING状态。自动苏醒时,进入RUNNABLE状态,而不是运行态。

id="embed_dom" src="https://www.processon.com/embed/5b8b888be4b0fe81b621d3b7">
  • 休眠期间,不释放任何资源,若中途被中断,则抛出InterruptedException。
  • 常被用来在线程中休眠,避免空跑,造成资源浪费。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值