Category : Java | Sub Category : Java Threads | By Prasad Bonam Last updated: 2021-04-20 03:51:42 Viewed : 582
This is a static method available in thread class and give the hint for the thread scheduler that the currently running thread is willing to yield the execution time for some other thread.
Thread.yield() will just give a recommendation/hint for the thread scheduler and it is up to thread scheduler to accept or ignore. Therefore we cannot guarantee that calling the Thread.yield() will immediately move the currently running thread not “Not Running state” and allow some other thread to utilize the CPU time. It will be decided by the thread scheduler.
Please refer the below example about Thread.yield()
It is very difficult to give the exact output upon running the above code sample as the Thread.yield() method is not guaranteed to run. Therefore if you run above program few times, you might get different results.