Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.
17th Nov 2019, 2:36 PM
LunarCoffee
LunarCoffee - avatar
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.
17th Nov 2019, 3:41 PM
zemiak