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

前端 未结 12 1808
野的像风
野的像风 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

    Yield: It is a hint (not guaranteed) to the scheduler that you have done enough and that some other thread of same priority might run and use the CPU.

    Thread.sleep();
    

    Sleep: It blocks the execution of that particular thread for a given time.

    TimeUnit.MILLISECONDS.sleep(1000);
    

提交回复
热议问题