Java: why Thread.sleep() and yield() are static?
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.
The same reason is why stop() and suspend() methods are deprecated. Intrusion in thread's state from outside is dangerous and can cause unpredictable result. And if sleep is not static, for example, how do you think interruption from it will happen?
This is because whenever you are calling these methods, those are applied on the same thread that is running.
You can't tell another thread to perform some operation like, sleep()
or wait
. All the operation are performed on the thread which is being executed currently.
If you call the yield
or sleep
method, it applies to whichever thread is currently executing, rather than any specific thread - you don't have to specify which thread is currently running to free up the processor.
similar thread in this forum