yield()和sleep()之间有什么区别? [英] what's the difference between yield() and sleep()?

查看:204
本文介绍了yield()和sleep()之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个区别:



如果我们说 thread.sleep(1000),该线程将会睡眠对于 1000 肯定是毫秒,而使用 yield()则没有这样的保证。这对于线程调度很有用,因为调用 yield()的线程可能会立即再次选择运行。



<还有什么?

解决方案

收益率只是说:现在是一个让另一个线程运行并给调度程序提示的好时机。 sleep 确实这样做:至少在给定时间内睡觉。


I know one difference:

If we say thread.sleep(1000), that thread will sleep for 1000 milliseconds for sure, whereas with yield() there is no such guarantee. This is useful for thread scheduling, since the thread which calls yield() may very well selected immediately again for running.

What else?

解决方案

yield merely says: now is a good time to let another thread run and is a hint to the scheduler. sleep really does that: sleep at least the given time.

这篇关于yield()和sleep()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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