Saturday, 10 June 2017

Java Tutorial: Java Threads (Thread yield) ~ foundjava


Click here to watch in Youtube :
MyRunnable.java
public class MyRunnable implements Runnable
{

    Thread t;

    public MyRunnable(String str)
    {

        t = new Thread(this, str);
        // this will call run() function
        t.start();
    }

    public void run()
    {
        for (int i = 0; i < 5; i++)
        {
            /*
             * Yields control to another thread every 5
             * iterations
             */
            if ((i % 5) == 0)
            {
                System.out.println(Thread.currentThread().getName()
                        + " yielding control...");

                /*
                 * Causes the currently executing thread
                 * object to temporarily pause and allow
                 * other threads to execute.
                 * 
                 * A hint to the scheduler that the current
                 * thread is willing to yield its current
                 * use of a processor. The scheduler is free
                 * to ignore this hint.
                 */
                Thread.yield();
            }

        }
        System.out.println(Thread.currentThread().getName()
                + " has finished executing.");
    }
}
ThreadDemo.java
public class ThreadDemo
{

    public static void main(String args[]) throws InterruptedException
    {
        new MyRunnable("Thread 1");
        new MyRunnable("Thread 2");
        new MyRunnable("Thread 3");
    }
}
Output
Thread 1 yielding control...
Thread 1 has finished executing.
Thread 2 yielding control...
Thread 3 yielding control...
Thread 3 has finished executing.
Thread 2 has finished executing.

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment