How to Use isAlive() and join() method in Java Threading

In our last blog we see how to multiple process works together. Now in this blog, we will see how check our process working on not using isAlive() method. We also see how to join thread in main program using join() method in Java Threading.

Return type of isAlive() method is Boolean. This is method is Thread class method. If this method returns true value then we can say that processing in run mode otherwise we can say process is completed or not start.

 

Let’s see example on isAlive() and join() method in Java Threading

 

package classStore;

public class Process1 implements Runnable

{

            String processName;

            Thread t;          // Ref

            public Process1(String processName)

            {

                        t = new Thread(this);   // intilization

                        this.processName = processName;

                        t.start();                                                // start thread process

            }

            // Child – 1: display 1 to 10

            // Child – 2: display 1 to 15

            // Child – 3: display 1 to 5

 

            public void run()

            {

                        try

                        {

                                    if(processName.compareTo(“Child-1”) == 0)

                                    {

                                                for(int i=1;i<=10;i++)

                                                {

                                                            Thread.sleep(1000);

                                                            System.out.println(processName +” : ” + i);

                                                }

                                    }

                                    else if(processName.compareTo(“Child-2”) == 0)

                                    {

                                                for(int i=1;i<=15;i++)

                                                {

                                                            Thread.sleep(1000);

                                                            System.out.println(processName +” : ” + i);

                                                }

                                    }

                                    else if(processName.compareTo(“Child-3”) == 0)

                                    {

                                                for(int i=1;i<=5;i++)

                                                {

                                                            Thread.sleep(1000);

                                                            System.out.println(processName +” : ” + i);

                                                }

                                    }

                        }

                        catch(Exception ex){}  

            }

}

 


 

package examples;

import classStore.*;

public class Thread6

 {

                public static void main(String[] args)

                {

                                Process1 obj1 = new Process1(“Child-1”);

                                Process1 obj2 = new Process1(“Child-2”);

                                Process1 obj3 = new Process1(“Child-3”);

 

                                // Want to check process is working on not

                                System.out.println(“Process – 1 is working ? ” + obj1.t.isAlive());

                                System.out.println(“Process – 2 is working ? ” + obj2.t.isAlive());

                                System.out.println(“Process – 3 is working ? ” + obj3.t.isAlive());

 

                                try

                                {

                                                Thread.sleep(6000);

                                }

catch(Exception ex){}

 

                                // Want to check process is working on not after 3 sec

 

                                System.out.println(“Process – 1 is working ? ” + obj1.t.isAlive());

                                System.out.println(“Process – 2 is working ? ” + obj2.t.isAlive());

                                System.out.println(“Process – 3 is working ? ” + obj3.t.isAlive());

 

                                try

                                {

                                                obj1.t.join();      // wait to complete process1

                                                obj2.t.join();  // wait to complete process2

                                                obj3.t.join();  // wait to complete process3

                                }

                                catch(Exception ex){}

 

                                System.out.println(“Process – 1 is working ? ” + obj1.t.isAlive());

                                System.out.println(“Process – 2 is working ? ” + obj2.t.isAlive());

                                System.out.println(“Process – 3 is working ? ” + obj3.t.isAlive());

                }

}

Output:

Check video for program output

 

Video also available on YouTube Java Channel


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

618total visits,3visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

One response to “How to Use isAlive() and join() method in Java Threading”

  1. This is the right site for anybody who would like to understand this topic.

    You realize a whole lot its almost hard to argue with you (not that I personally will
    need to…HaHa). You certainly put a brand new spin on a topic that has been discussed
    for a long time. Great stuff, just great!