You are here:   HomeJava ProgrammingMultithreadingExplain Java Thread Model
by Dinesh Thakur Category: Multithreading

 

class isAliveAndJoin1  implements Runnable

      {
          Thread t;
          isAliveAndJoin1(String n)
             {
             t=new Thread(this,n);
             System.out.println("Thread is :"+t);
             }
             public void run()
                {
                    try
                      {
                         for(int i=1;i<=5;i++)
                           {
                         System.out.println(t.getName()+":"+i);
                         t.sleep(200);
                           }
                      }                
                          catch(InterruptedException e)
                                       {
                                  System.out.println(t.getName()+" is Interrupted");
                               }
                }
      }
                    class isAliveAndJoin2 implements Runnable
                    {
                         Thread t;
                         isAliveAndJoin2(String n)
                          {
                              t=new Thread(this,n);
                              System.out.println("Thread is :"+t);
                          }
                                      public void run()
                                   {
                                     try
                                       {
                                       for(int i=1;i<=10;i++)
                                           {
                                              System.out.println(t.getName()+":"+i);
                                              t.sleep(200);
                                            }
                                            }
                                             catch(InterruptedException e)
                                                    {
                                                    System.out.println(t.getName()+" is Interrupted");
                                                    }
                                      }
                        }
                                        class isAliveAndJoin
                                          {
                                              public static void main(String args[])
                                        {
                                           isAliveAndJoin1 k1=new isAliveAndJoin1("AWT Thread");
                                           isAliveAndJoin2 k2=new isAliveAndJoin2("Swing Thread");
                                           k1.t.start();
                                           k2.t.start();
                                           while(k1.t.isAlive() && k2.t.isAlive())
                                                 {
                                                    try 
                                                      {
                                                     Thread.sleep(400);
                                                      }
                                                    catch(InterruptedException e)
                                                           {
                                                            }
                                                             try
                                                            {
                                                               k1.t.join();
                                                               k2.t.join();
                                                            }
                                                               catch(InterruptedException e)
                                                                       {
                                                                       }
                                                 }
                                                                                 if(!k1.t.isAlive())
                                                             System.out.println("AWT Thread is Over");
                                                             if(!k2.t.isAlive())
                                                             System.out.println("Swing Thread is Over");
                                       }
                                          }

isAlive() and Join() Example in Java By Implementing Runnable Interface

What's New and Popular