A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Program for Yield()

class MyThread extends Thread
{
    public void run()
    {
        for(int i=0; i<10; i++)
        {
            System.out.println("child thread");
            Thread.yield();
            /* we comment this line, then both threads will execute simultaneously and 
            we can't expect which thread will complete first.
            if we not comment then main thread will get the more chance.           
            */
        }
    }
}
class Yield
{
    public static void main(String args[])
    {
        Thread t=new Thread();
        t.start();
        for(int i=0; i<10;i++)
        {
            System.out.println("This is main Thread");
        }
    }
}

output:


LEARN TUTORIALS

.

.