Saturday, 10 June 2017

Java Tutorial : Java Threads (Thread Sleep | Thread sleep in java) ~ foundjava


Click here to watch in Youtube :

Click the below Image to Enlarge
Java Tutorial : Java Threads (Thread Sleep | Thread sleep in java) 
DisplayThread.java
class DisplayThread extends Thread
{
    public DisplayThread(String threadName)
    {
        super(threadName);
    }

    public static void main(String args[])
    {
        DisplayThread displayThread1 = new DisplayThread("DisplayThread-1");
        DisplayThread displayThread2 = new DisplayThread("DisplayThread-2");

        displayThread1.start();
        displayThread2.start();
    }

    public void run()
    {

        System.out.println("Before sleep of = " + this.getName());
        try
        {
            /*
             * Causes the currently executing thread to
             * sleep (temporarily cease execution) for the
             * specified number of milliseconds, subject to
             * the precision and accuracy of system timers
             * and schedulers. The thread does not lose
             * ownership of any monitors.
             */
            Thread.sleep(10000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println("After sleep of = " + this.getName());
    }

}
Output
Before sleep of = DisplayThread-1
Before sleep of = DisplayThread-2
After sleep of = DisplayThread-2
After sleep of = DisplayThread-1

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment