by Dinesh Thakur Category: Multithreading

class NewThreadDemo implements Runnable


{
            Thread t;
            NewThreadDemo(String threadName)
      {
            t=new Thread(this, threadName);
            System.out.println("New userdefined child thread created" +threadName);
            t.start();
       }

                 public void run()
            {
                try
                 {
                      for(int i = 1; i <=10; i++)
                       {
                           System.out.println("Child Thread: [" +Thread.currentThread().getName()
+ "] " + i);
                           Thread.sleep(500);
                        }
                  }
                      catch (InterruptedException e)
                         {
                              System.out.println("UseDefine Child interrupted.");
                          }
                      System.out.println("Exiting UserDeinfed child thread.");
               }
}
class MultiThreadDemo
      {
                    public static void main(String args[])
               {
                    NewThreadDemo ch1 = new NewThreadDemo("Child_One");
                    NewThreadDemo ch2 = new NewThreadDemo("Child_Two");
                    NewThreadDemo ch3 = new NewThreadDemo("Child_Three");
                    NewThreadDemo ch4 = new NewThreadDemo("Child_FOur");

                    System.out.println("Child Thread One is alive: " + ch1.t.isAlive());
                    System.out.println("Child Thread Two is alive: "+ ch2.t.isAlive());
                    System.out.println("Child Thread Three is alive: "+ ch3.t.isAlive());
                    System.out.println("Child Thread Four is alive: "+ch4.t.isAlive());

                     try
                      {
                         System.out.println("Waiting for child threads to finish.");
                         ch1.t.join ();
                         ch2.t.join ();
                         ch3.t.join ();
                         ch4.t.join ();
                       }

                           catch (InterruptedException e)
                                {

                                     System.out.println("Main thread Interrupted");
                                }
                              System.out.println("Child Thread One is alive: " + ch1.t.isAlive());
                              System.out.println("Child Thread Two is alive: "+ ch2.t.isAlive());
                              System.out.println("Child Thread Three is alive: "+ ch3.t.isAlive());
                              System.out.println("Child Thread Four is alive: "+ch4.t.isAlive());
                              System.out.println("Main thread exiting.");

                  }
      }

In the code all child thread are join(). If see the output, the main thread exited in the mid of the running code. It wait for all child to be exit. At last it exits from the code.

 

Output

 

join () and isAlive ()



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article