What is difference between sleep() method and yield() method of multi threading?

前端 未结 12 1764
野的像风
野的像风 2020-12-22 17:02

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

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-22 17:25

    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()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.

提交回复
热议问题