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

前端 未结 4 1307
情书的邮戳
情书的邮戳 2021-01-01 00:31

I know one difference:

If we say thread.sleep(1000), that thread will sleep for 1000 milliseconds for sure, whereas with yield()

4条回答
  •  臣服心动
    2021-01-01 00:41

    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.

    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.

提交回复
热议问题