编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

多线程的sleep()方法和yield()方法之间有什么区别?:What is difference between sleep() method and yield() method of multi threading?

Eice-zz 线程 2022-5-13 10:36 56人围观

腾讯云服务器
多线程的sleep()方法和yield()方法之间有什么区别?的处理方法

当前正在执行的线程遇到调用 yield() 线程进入可运行状态/就绪状态.

As currently executing thread while it encounters the call sleep() then thread moves immediately into sleeping stat. Whereas for yield() thread moves into runnable state / ready state.

问题解答

sleep()导致线程在给定的时间内肯定停止执行;如果不需要运行其他线程或进程,则CPU将处于空闲状态(并可能进入节能模式).

sleep() causes the thread to definitely stop executing for a given amount of time; if no other thread or process needs to be run, the CPU will be idle (and probably enter a power saving mode).

yield()基本上意味着线程没有做任何特别重要的事情,如果需要运行任何其他线程或进程,则应该这样做.否则,当前线程将继续运行.

yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should. Otherwise, the current thread will continue to run.

这篇关于多线程的sleep()方法和yield()方法之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^