Java中的Thread.sleep()– Java线程睡眠

Java中的Thread.sleep (Thread.sleep in Java)

Thread.sleep() method can be used to pause the execution of current thread for specified time in milliseconds. The argument value for milliseconds can’t be negative, else it throws IllegalArgumentException.

Thread.sleep()方法可用于在指定时间(毫秒)内暂停当前线程的执行。 以毫秒为单位的参数值不能为负,否则会抛出IllegalArgumentException

There is another overloaded method sleep(long millis, int nanos) that can be used to pause the execution of current thread for specified milliseconds and nanoseconds. The allowed nano second value is between 0 and 999999.

还有另一种重载方法sleep(long millis, int nanos) ,可用于将当前线程的执行暂停指定的毫秒和纳秒。 允许的纳秒值在0到999999之间。

Java线程睡眠示例 (Java Thread Sleep Example)

Here is a simple program where Thread.sleep() is used to pause the main thread execution for 2 seconds.

这是一个简单的程序,其中Thread.sleep()用于将主线程执行暂停2秒钟。

ThreadSleep.java

ThreadSleep.java

package com.journaldev.threads;

public class ThreadSleep {

    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread.sleep(2000);
        System.out.println("Sleep time in ms = "+(System.currentTimeMillis()-start));
        
    }

}

If you will run the above program, you will notice that the thread sleep time it prints is slightly greater than 2000. This is caused by how thread sleep works and operating system specific implementation of thread scheduler.

如果您将运行上述程序,则会注意到它打印的线程睡眠时间略大于2000。这是由于线程睡眠的工作方式以及特定于操作系统的线程调度程序的实现所致。

Java线程睡眠要点 (Java Thread Sleep important points)

  1. It always pause the current thread execution.

    它总是暂停当前线程的执行。
  2. The actual time thread sleeps before waking up and start execution depends on system timers and schedulers. For a quiet system, the actual time for sleep is near to the specified sleep time but for a busy system it will be little bit more.

    线程在唤醒和开始执行之前Hibernate的实际时间取决于系统计时器和调度程序。 对于安静的系统,实际的睡眠时间接近于指定的睡眠时间,但是对于繁忙的系统,实际时间会更多。
  3. Thread sleep doesn’t lose any monitors or locks current thread has acquired.

    线程Hibernate不会丢失任何监视器,也不会锁定当前已获取的线程。
  4. Any other thread can interrupt the current thread in sleep, in that case InterruptedException is thrown.

    任何其他线程都可以在睡眠中中断当前线程,在这种情况下会抛出InterruptedException。

线程睡眠如何工作 (How Thread Sleep Works)

Thread.sleep() interacts with the thread scheduler to put the current thread in wait state for specified period of time. Once the wait time is over, thread state is changed to runnable state and wait for the CPU for further execution. So the actual time that current thread sleep depends on the thread scheduler that is part of operating system.

Thread.sleep()与线程调度程序进行交互,以在指定的时间段内将当前线程置于等待状态。 等待时间结束后,线程状态将更改为可运行状态,并等待CPU进一步执行。 因此,当前线程睡眠的实际时间取决于操作系统中的线程调度程序。

翻译自: https://www.journaldev.com/1020/thread-sleep-java

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 目录 1. 2. 目录 .........................................................................................................................................................1 JVM ...................................................................面试.................................................................................... 19 2.1. 线程 ...................................................................................................................................................... 20 2.2. JVM 内存区域 ..................................................................................................................................... 21 2.2.1. 程序计数器(线程私有) ................................................................................................................ 22 2.2.2. 虚拟机栈(线程私有) .................................................................................................................... 22 2.2.3. 本地方法区(线程私有) ................................................................................................................ 23 2.2.4. 堆(Heap-线程共享)-运行时数据区 ...................................................................................... 23 2.2.5. 方法区/永久代(线程共享) ..................................................................................................... 23 2.3. JVM 运行时内存 ................................................................................................................................. 24 2.3.1. 新生代 .......................................................................................................................................... 24 2.3.1.1. 2.3.1.2. 2.3.1.3. 2.3.1.4. Eden 区 .................................................................................................................................................... 24 ServivorFrom........................................................................................................................................... 24 ServivorTo ...........................................

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值