by Dinesh Thakur

We first make a class MultipleThreads which extends Thread class and then we create an instance of that class t1 and pass the string "Child Thread" as parameter to it which assigns the name of the thread as Child Thread and also displays the information of the child thread on the screen. After that the start() method is invoked on that object which initiates the run() method. Before executing run() method of MultipleThreads, control returns to main(). In main() method, the for loop is executed which is supposed to print Mainthread : 1

Since, the main() must be the last thread to finish, its delay time (1000msec) is set more than the delay time of child thread (500msec). It is done so because if in case the main() thread finishes before the child threads gets over, the system may hang. So, after displaying the message Main thread : I, the main thread goes to sleep for 1000 msec. During this period, run() method of new thread is invoked which displays messages:

Child thread: 1

Child thread: 2

with a gap of 500msec in between.

The moment the period of 1000 msec gets over, the main thread becomes active again which displays the message:

Main thread: 2

After that, again the rnain thread goes to sleep for the period of 1000msec during which again the run() method of new thread is invoked which displays:

Child thread: 3

Child thread : 4

class MultipleThreads extends Thread

        {
            MultipleThreads(String n)
             {
               super(n);
               System.out.println("Child Thread is : "+getName());
             }
                   public void run()
                       {
                      try
                       {
                          for(int i=1;i<=5;i++)
                          {
                             System.out.println("Child Thread:"+i);
                             sleep(500);
                          }
                            }
                                     catch(InterruptedException e)
                                    {
                                    System.out.println("Child Thread is Interrupted");
                                    }
                  }
         }
                    class MultipleThreadsExample
                          {
                             public static void main(String args[])
                                   {
                                       MultipleThreads t1=new MultipleThreads("Child Thread");
                               t1.start();
                                       try
                                 {
                                    for(int i=1;i<=5;i++)
                                       {
                                      System.out.println("Main Thread:"+i);
                                          Thread.sleep(1000);
                                       }
                                         }
                                                  catch(InterruptedException e)
                                           {
                                              System.out.println("Main Thread Interrupted");
                                           }
                                  }
                        }          

Multiple Threads in Java Example