Skip to content
Advertisement

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

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement