Java Interview Questions

BY Author

Java – Yield Method

The yield() method is used by the running thread to provide a chance to other waiting threads of same priority for their execution.If two or more waiting threads have the same priority, then thread scheduler decides their execution sequence.If waiting threads have low priority than running thread, they do not get the chance to execute until the running thread completes its work.

In the above program, main thread and Child thread have the same priority. Therefore by calling yield() method on the main thread, main thread gives most of the chance to child thread to execute. Hence Child thread gets execute earlier than the main thread.