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

查看:68
本文介绍了多线程的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()方法之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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