Saturday, 10 June 2017

Java Tutorial : Java Threads (What if we call the run() method directly instead of start() method) ~ foundjava


Click here to watch in Youtube :

Click the below Image to Enlarge
Java Tutorial : Java Threads (What if we call the run() method directly instead of start() method) 
Java Tutorial : Java Threads (What if we call the run() method directly instead of start() method) 
DisplayThread.java
class DisplayThread extends Thread
{

    public static void main(String args[])
    {
        DisplayThread displayThread = new DisplayThread();

        /*
         * fine, but does not start a separate call stack
         */
        displayThread.run();

    }

    public void run()
    {
        System.out.println("Hello by ");
    }

}
Output
Hello by 

DisplayNumberThread.java
public class DisplayNumberThread extends Thread
{

    public static void main(String[] args)
    {
        DisplayNumberThread displayNumberThread1 = new DisplayNumberThread();
        DisplayNumberThread displayNumberThread2 = new DisplayNumberThread();
        displayNumberThread1.run();
        displayNumberThread2.run();

    }

    public void run()
    {
        for (int i = 1; i < 5; i++)
        {
            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            System.out.println(i);
        }
    }

}
Output
1
2
3
4
1
2
3
4

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment