You are here:   HomeJava ProgrammingMultithreadingSleep Method in Thread in Java Example
by Dinesh Thakur Category: Multithreading

 

class InheritingThreadClass1 extends Thread

      {
          InheritingThreadClass1(String n)
              {
              super(n);
              System.out.println("Thread is :"+getName());
              }
              public void run()
                 {
                   try
                      {
                         for(int i=1;i<=5;i++)
                             {
                             System.out.println(getName()+":"+i);
                             sleep(200);
                         }
                          }
                           catch(InterruptedException e)
                              {
                             System.out.println(getName()+" is Interrupted");
                          }
                     }
      }
                    class InheritingThreadClass2 extends Thread
                      {
                       InheritingThreadClass2(String n)
                         {
                          super(n);
                          System.out.println("Thread is:"+getName());
                         }
                                  public void run()
                            {
                              try
                                  {
                                    for(int i=1;i<=10;i++)
                                        {
                                       System.out.println(getName()+":"+i);
                                       sleep(200);
                                        }
                                  }
                                       catch(InterruptedException e)
                                             {
                                                System.out.println(getName()+" is Interrupted");
                                             }
                                     }
                     }
                                   class isAliveJoinExample
                                  {
                                      public static void main(String args[])
                                         {
                                         InheritingThreadClass1 t1=new InheritingThreadClass1("AWT Thread");
                                         InheritingThreadClass2 t2=new InheritingThreadClass2("Swing Thread");
                                         t1.start();
                                         while(t1.isAlive() && t2.isAlive())
                                            {
                                              try
                                                 {
                                                Thread.sleep(400);
                                                 }
                                                                catch(InterruptedException e)
                                                     {
                                                     }
                                                       try
                                                          {
                                                             t1.join();
                                                             t2.join();
                                                          }
                                                             catch(InterruptedException e)
                                                              {
                                                              }
                                                }
                                                            if(!t1.isAlive())
                                               System.out.println("AWT Thread is Over");
                                               if(!t2.isAlive())
                                                          System.out.println("Swing Thread is Over");
                                                 }
                                          }    

isAlive() and Join() Example in Java By Inheriting Thread Class



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes 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.



What's New and Popular





Popular Article