I know one difference:
If we say thread.sleep(1000)
, that thread will sleep for 1000
milliseconds for sure, whereas with yield()
there is no such guarantee. This is useful for thread scheduling, since the thread which calls yield()
may very well selected immediately again for running.
What else?
t.interrupt()
InterruptedException
.yield()
is much based on the Thread Priorities concept. (All thread are assigned priorities and when a thread of higher priority is in Runnable state it ususally preempts / stops execution of lower priority threads depending on implementation of ThreadScheduler.)Note:
Thread.sleep()
and Thread.yield()
are static functions and affect the current thread executing it.yield
merely says: now is a good time to let another thread run and is a hint to the scheduler. sleep
really does that: sleep at least the given time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With