Java Thread.sleep()实现 [英] Java Thread.sleep() implementation

查看:133
本文介绍了Java Thread.sleep()实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我了解Thread.sleep()函数的实现方式吗?当经过指定的时间或其他一些线程中断时,线程将恢复/唤醒.我有兴趣了解其工作背后的设计模式.

Can someone help me understand how the Thread.sleep() function is implemented? A thread resumes/wakes up when the specified time elapses or when some other thread interrupts. I'm interested in understanding the design pattern behind the working of this.

据说睡眠对CPU消耗没有影响.当前线程是否已添加到侦听器列表?何时检查中断标志?调度程序是否继续检查每"x"次(根据操作系统支持的时间)正在休眠的每个线程的中断状态?线程如何在不影响CPU的情况下取回控件.

Sleep is said to have no effect on the CPU consumption. Is the current thread added to a list of listeners? When will the check for interrupt flag occur? Does the scheduler keep checking for the interrupt status of every thread that is sleeping for every "x" amount of time (based on what the OS supports)? How does the thread get the control back without effecting the CPU.

我已经搜索了.抱歉,如果我错过了任何容易找到的链接.

I have searched for this. Sorry if I missed any link that is easy to find.

推荐答案

有人可以帮助我了解Thread.sleep()函数的实现方式吗?

Can someone help me understand how the Thread.sleep() function is implemented?

它在操作系统提供的基础本机线程上调用sleep.

It calls sleep on the underlying native thread provided by the operating system.

据说睡眠对CPU消耗没有影响.

Sleep is said to have no effect on the CPU consumption.

未运行的线程不会消耗CPU时间.

A thread that is not running does not consume CPU time.

当前线程是否已添加到侦听器列表中?

Is the current thread added to a list of listeners?

不.

何时会出现中断标志检查?

When will the check for interrupt flag occur?

该线程无法运行,因此无法检查中断标志.如果需要,操作系统可以唤醒线程.

The thread cannot check the interrupt flag because it is not running. The operating system can wake the thread if requested.

调度程序是否一直在检查每个"x"时间(基于操作系统支持的时间)内正在休眠的每个线程的中断状态?

Does the scheduler keep checking for the interrupt status of every thread that is sleeping for every "x" amount of time (based on what the OS supports)?

否.

线程如何在不影响CPU的情况下取回控件.

How does the thread get the control back without effecting the CPU.

该时间到期后,操作系统会自动唤醒该线程,或者另一个线程可以要求操作系统提早唤醒它.

The thread is automatically woken by the operating system when the time expires, or another thread can ask the operating system to wake it early.

这是

这篇关于Java Thread.sleep()实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆