Java: why Thread.sleep() and yield() are static?

后端 未结 6 406
庸人自扰
庸人自扰 2021-01-03 19:56

Why sleep() and yield() methods are defined as static methods in java.lang.Thread class?

6条回答
  •  长情又很酷
    2021-01-03 20:53

    The code would only execute when someXThread was executing, in which case telling someYThread to yield would be pointless. So since the only thread worth calling yield on is the current thread, they make the method static so you won't waste time trying to call yield on some other thread.

提交回复
热议问题