Saturday, 10 June 2017

Java Tutorial: Java Threads (Thread join in java | Thread join [ms]) ~ foundjava


Click here to watch in Youtube :

DisplayNumberThread.java
class DisplayNumberThread extends Thread
{
    public static void main(String args[])
    {
        DisplayNumberThread dnt1 = new DisplayNumberThread();
        DisplayNumberThread dnt2 = new DisplayNumberThread();
        DisplayNumberThread dnt3 = new DisplayNumberThread();
        dnt1.start();
        try
        {
            /*
             * Waits at most millis milliseconds for this
             * thread to die.
             */
            dnt1.join(2500);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        dnt2.start();
        dnt3.start();
    }

    public void run()
    {
        for (int i = 1; i <= 5; i++)
        {
            try
            {
                Thread.sleep(500);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            System.out.println(this.getName() + " = " + i);
        }
    }
}
Output
Thread-0 = 1
Thread-0 = 2
Thread-0 = 3
Thread-0 = 4
Thread-0 = 5
Thread-1 = 1
Thread-2 = 1
Thread-2 = 2
Thread-1 = 2
Thread-1 = 3
Thread-2 = 3
Thread-2 = 4
Thread-1 = 4
Thread-1 = 5
Thread-2 = 5

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment