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

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

    Both methods are used to prevent thread execution. But specifically, sleep(): purpose:if a thread don't want to perform any operation for particular amount of time then we should go for sleep().for e.x. slide show . yield(): purpose:if a thread wants to pause it's execution to give chance of execution to another waiting threads of same priority.thread which requires more execution time should call yield() in between execution.

    Note:some platform may not provide proper support for yield() . because underlying system may not provide support for preemptive scheduling.moreover yield() is native method.

提交回复
热议问题