+7

What does yield method of the Thread class do?

According to me this is right or not A yield () method moves the currently running thread to a  runnable state and allows the other threads for execution. So that equal priority threads have a chance to run. It is a static method. It doesn’t release any lock. Yield () method moves the thread back to the Runnable state only, and not the thread to sleep (), wait () (or) block.

11/16/2019 7:18:07 PM

₦ØØⱤ ₳Ⱡł

2 Answers

New Answer

+2

When you use multiple threads, there exists a scheduler which effectively pauses and resumes execution of threads. This largely cannot be controlled by the programmer in Java (AFAIK), though Thread.yield() is meant to tell the scheduler that the current thread can be stopped to allow another thread to run, though the scheduler has no responsibility to honor that. Your thoughts seem to be mostly correct, though the scheduler will often not do anything. The documentation (https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#yield() ) suggests that use of the method be limited in cases where profiling shows marked improvements. I wouldn't recommend you use it for anything on Sololearn, in the least.

0

Thread.yield() informs the sheduler that the current thread temporarily does not need to use the CPU. The sheduler can then decide if stops it and when to runs it again or may not respond in general. It is an optimization service, although success is not always guaranteed.