Saturday, 10 June 2017

Java Tutorial : Java Threads (Thread start method) ~ foundjava


Click here to watch in Youtube :

Click the below Image to Enlarge
Java Tutorial : Java Threads (Thread start method) 
ThreadExample.java
public class ThreadExample
{
    public static void main(String[] args)
    {
        /*
         * It prints out the name of the thread executing
         * the main() method. This thread is assigned by the
         * JVM.
         */
        System.out.println(Thread.currentThread().getName());

        /*
         * It starts up 10 threads and give them all a
         * number as name ("" + i). Each thread then prints
         * its name out, and then stops executing.
         */
        for (int i = 0; i < 10; i++)
        {
            new Thread("" + i)
            {
                public void run()
                {
                    try
                    {
                        Thread.sleep(100);
                    }
                    catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    System.out.println("Thread: " + getName() + " running");
                }
            }.start();
        }
    }
}
Output
main
Thread: 9 running
Thread: 2 running
Thread: 5 running
Thread: 6 running
Thread: 1 running
Thread: 8 running
Thread: 3 running
Thread: 4 running
Thread: 7 running
Thread: 0 running

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment