Publish your story - poem - jokes free online--click here

JAVA

Joining of Thread in Java

Joining of multi thread (use of sleep(), isAlive(), setname())

                          Join() method is used to join the threads with the main thread. See what happens there is always a thread for every methods which is getting executed.
                   Hence main() has also its own thread. In normal cases main thread gets finished before the other thread. Using join method we can make the main to wait for the termination of other threads.
Try to understand the following program:--

import java.util.*;
class cls1 extends Thread
{
          public void run()
          {
                   System.out.println(getName()+" started");
                   for(int i=1;i<=5;i++)
                   {
                             try
                             {
                                      System.out.println(getName()+" "+i);
                                      sleep(3000); //suspends(stop) the thread for 3 seconds
                             }
                             catch(InterruptedException e)
                             {
                                      System.out.println("thread interrupted");
                             }
                   }
          }
}
class cls2 extends Thread
{
          public void run()
          {
                   System.out.println(getName()+" started");
                   for(int i=1;i<=5;i++)
                   {
                             try
                             {
                                      System.out.println(getName()+" "+i);
                                      sleep(2000); //suspends(stop) the thread for 2 seconds

                             }
                             catch(InterruptedException e)
                             {
                                      System.out.println("thread interrupted");
                             }
                   }
          }
}
class check
{
          void show(Thread t)
          {
                   if(t.isAlive())//checking status of thread
                   {
                             System.out.println(t.getName()+" is still running");
                   }
                   else
                   {
                             System.out.println(t.getName()+" has stopped");
                   }
          }
}
public class abc
{
          public static void main(String as[])
          {
                   System.out.println("main thread starts");
                   Thread t1=new cls1();
                   Thread t2=new cls2();
                   check c =new check();
                   t1.setName("one"); //sets the name as “one”
                   t2.setName("two"); //sets the name as “two”
                   t1.start(); //thread one starts
                   t2.start(); //thread two starts
                   c.show(t1);
                   c.show(t2);
                   System.out.println("main thread will wait here to finish the both thread");
                   try
                   {
                             t1.join(); //thread one join the main thread
                             t2.join(); //thread two join the main thread
                   }
                   catch(InterruptedException e)
                   {
                             System.out.println("there is some thing error");
                   }
                   c.show(t1);
                   c.show(t2);System.out.println("main thread finished");
          }
}


No comments:

Post a Comment