java yield() 和 sleep() 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5304324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:31:02  来源:igfitidea点击:

what's the difference between yield() and sleep()?

javamultithreading

提问by Bhushan

I know one difference:

我知道一个区别:

If we say thread.sleep(1000), that thread will sleep for 1000milliseconds 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.

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

What else?

还有什么?

回答by frictionlesspulley

Thread.sleep()

线程.sleep()

  1. The current threadchanges state from Runningto Waiting/Blocked as shown in the diagram below.
  2. Any other thread with reference to the thread currently sleeping (say t) can interrupt it calling t.interrupt()
    • the call to sleep has to be encapsulated to catch the checked exception of InterruptedException
  3. After the time period for which the thread was set to sleep it goes to the Runnable state and might not run immediately!It has to wait for the Thread Scheduler to schedule it for its time slice.
  1. 当前的线程从改变状态运行等待/阻止如下面的图所示。
  2. 参考当前休眠的线程(例如 t)的任何其他线程都可以中断它调用 t.interrupt()
    • 必须封装对 sleep 的调用以捕获已检查的异常 InterruptedException
  3. 在线程设置为休眠的时间段之后,它会进入 Runnable 状态并且可能不会立即运行!它必须等待线程调度程序为其时间片进行调度。

Thread.yield()

线程.yield()

  1. Calling it may cause the Thread Scheduler to move the current threadfrom Runningto Runnablestate and execute another same priority thread which was in Runnable state. This transition of state takes place only if there is some other thread of same priority in Runnable state. Hence the no guaranteethat the thread will stop execution as the criteria of another same priority thread might not be met.
  2. .yield()is much based on the Thread Priorities concept. (All thread are assigned priorities and when a thread of higher priority is in Runnable state it ususally preempts / stops execution of lower priority threads depending on implementation of ThreadScheduler.)
  1. 调用它可能会导致线程调度器将当前线程Running状态移动到Runnable状态并执行另一个处于 Runnable 状态的相同优先级线程。仅当在 Runnable 状态中存在其他具有相同优先级的线程时,才会发生这种状态转换。因此,不能保证线程将停止执行,因为可能不满足另一个相同优先级线程的标准。
  2. .yield()很大程度上基于线程优先级概念。(所有线程都分配了优先级,当更高优先级的线程处于 Runnable 状态时,它通常会根据 ThreadScheduler 的实现抢占/停止低优先级线程的执行。)

enter image description hereNote:

在此处输入图片说明笔记:

  • both Thread.sleep()and Thread.yield()are static functions and affect the current thread executing it.
  • both the functions will not let go the synchronized locks they hold.
  • 这两个Thread.sleep()Thread.yield()是静态的功能和影响当前线程执行它。
  • 这两个函数都不会放开它们持有的同步锁。

回答by DarkDust

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

yield只是说:现在是让另一个线程运行的好时机,并且是对调度程序的一个提示。sleep真的做到了:至少在给定的时间内睡觉。

回答by Reynaldo

yield() pauses momentarily the current thread, allowing the Thread Scheduler to execute other threads with the same priority. If there are no other threads waiting or their priority is lower, the yielded thread returns to its execution at once.

yield() 暂时暂停当前线程,允许线程调度程序执行具有相同优先级的其他线程。如果没有其他线程在等待或它们的优先级较低,则被让出的线程立即返回执行。

sleep() forces the current thread to halt its execution for a defined slot of time. Other waiting threads will start executing by taking advantage of this pause, that is, following the Thread Scheduler policy - whose implementation is vendor dependent.

sleep() 强制当前线程在定义的时间段内停止执行。其他等待线程将利用此暂停开始执行,即遵循线程调度程序策略 - 其实现取决于供应商。

回答by user541686

It's not"for sure" -- it could even take an hour for your thread to get another chance to run, depending on the operating system's thread scheduling algorithm, and the presence of higher-priority threads.

不是“肯定的”——您的线程甚至可能需要一个小时才能再次运行,这取决于操作系统的线程调度算法以及更高优先级线程的存在。

The only thing yield()does is say, "Okay, I'm kind of done, so feel free to end my time slice and continue executing something else." sleep, on the other hand, says "Wake me up in X milliseconds". sleepis used for waiting, the other one for giving others a chance to run. They're not alternatives.

唯一要做yield()的就是说,“好吧,我已经完成了,所以请随时结束我的时间片并继续执行其他事情。” sleep,另一方面,说“在 X 毫秒内唤醒我”。sleep用来等待,另一个用来给别人跑的机会。它们不是替代品。