isAlive() and join() methods of Thread Class in Java

How can one thread know when another thread has ended?
Thread provides a means by which you can answer this question. Java multi-threading provides two ways to find that

  1. isAlive() : It tests if this thread is alive. A thread is alive if it has been started and has not yet died. There is a transitional period from when a thread is running to when a thread is not running. After the run() method returns, there is a short period of time before the thread stops. If we want to know if the start method of the thread has been called or if thread has been terminated, we must use isAlive() method. This method is used to find out if a thread has actually been started and has yet not terminated.

    General Syntax :

    final boolean isAlive( )
    Return Value:  returns true if the thread upon 
    which it is called is still running. It returns
    false otherwise.
  2. join() : When the join() method is called, the current thread will simply wait until the thread it is joining with is no longer alive.
    Or we can say the method that you will more commonly use to wait for a thread to finish is called join( ). This method waits until the thread on which it is called terminates. Its name comes from the concept of the calling thread waiting until the specified thread joins it. Additional forms of join( ) allow you to specify a maximum amount of time that you want to wait for the specified thread to terminate.
    Syntax :

    final void join( ) throws InterruptedException

    Here is an improved version of the preceding example that uses join( ) to ensure that the main thread is the last to stop. It also demonstrates the isAlive( ) method.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // isAlive()
      
    public class oneThread extends Thread {
        public void run()
        {
            System.out.println("geeks ");
            try {
                Thread.sleep(300);
            }
            catch (InterruptedException ie) {
            }
            System.out.println("forgeeks ");
        }
        public static void main(String[] args)
        {
            oneThread c1 = new oneThread();
            oneThread c2 = new oneThread();
            c1.start();
            c2.start();
            System.out.println(c1.isAlive());
            System.out.println(c2.isAlive());
        }
    }

    chevron_right

    
    

    Output:

    geeks 
    true
    true
    geeks 
    forgeeks 
    forgeeks
    

    Example of thread without join()

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
    // thread without join()
      
    public class oneThread extends Thread {
        public void run()
        {
            System.out.println("geeks ");
            try {
                Thread.sleep(500);
            }
            catch (InterruptedException ie) {
            }
            System.out.println("forgeeks ");
        }
        public static void main(String[] args)
        {
            oneThread c1 = new oneThread();
            oneThread c2 = new oneThread();
            c1.start();
            c2.start();
        }
    }

    chevron_right

    
    

    Output:

    geeks 
    geeks 
    forgeeks 
    forgeeks 
    

    Example of thread with join()

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate
      
    public class oneThread extends Thread {
        public void run()
        {
            System.out.println("geeks ");
            try {
                Thread.sleep(300);
            }
            catch (InterruptedException ie) {
            }
            System.out.println("forgeeks ");
        }
        public static void main(String[] args)
        {
            oneThread c1 = new oneThread();
            oneThread c2 = new oneThread();
            c1.start();
      
            try {
                c1.join(); // Waiting for c1 to finish
            }
            catch (InterruptedException ie) {
            }
      
            c2.start();
        }
    }

    chevron_right

    
    

    Output:

    geeks 
    forgeeks 
    geeks 
    forgeeks
    

    This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



    My Personal Notes arrow_drop_up


    Article Tags :
    Practice Tags :


    2


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.