Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

What is the purpose of join() method in Thread class?

Pragya Keshap answered on February 4, 2023 Popularity 6/10 Helpfulness 3/10

Contents


More Related Answers

  • String joiner in streams java
  • string.join java 8
  • thread join python
  • C++ Join thread
  • Java String join
  • convert an array into a string using the join method
  • String join in java
  • Java program for joining the strings Using String.join() method
  • string.join java 8
  • Explain how join method works?
  • Explain how join method works?
  • string join method

  • What is the purpose of join() method in Thread class?

    0

    In Java, Thread Scheduler controls thread scheduling. But we can

    use join() method on a thread to make current thread to wait for

    another thread to finish.

    When we use join(), the current thread stops executing. It wait for

    the thread on which join() is called to finish.

    This makes sure that current thread will continue only after the

    thread it joined finished running. Consider following example:

    Public class ThreadJoin {

    Thread importantThread = new Thread(

    new Runnable() {

    public void run () {

    //do something

    }

    }

    );

    Thread currentThread = new Thread(

    new Runnable() {

    public void run () {

    //do something

    }

    }

    );

    importantThread.start(); // Line 1

    importantThread.join(); // Line 2

    currentThread.start(); // Line 3

    }

    In the above example, main thread is executing. On Line 1, a new

    thread called importantThread is ready to run. But at Line 2, main

    thread joins the importantThread. Now it lets importantTread to

    finish and then it moves to Line 3. So currentThread at Line 3 will

    not start till the importantThread has finished

    https://www.geeksforgeeks.org/joining-threads-in-java/

    Popularity 6/10 Helpfulness 3/10 Language java
    Source: Grepper
    Tags: java
    Link to this answer
    Share Copy Link
    Contributed on Feb 04 2023
    Pragya Keshap
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.