How to join thread in java program

class TestJoinMethod1 extends Thread
{
    public void run()
    {
        for (int i = 1; i <= 5; i++)
        {
            try
            {
                Thread.sleep(500);
            }
            catch (Exception e)
            {
                System.out.println(e);
            }
            System.out.println(i);
        }
    }

    public static void main(String args[])
    {
        TestJoinMethod1 t1 = new TestJoinMethod1();
        TestJoinMethod1 t2 = new TestJoinMethod1();
        TestJoinMethod1 t3 = new TestJoinMethod1();

        t1.start();
        try
        {
            t1.join();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }

        t2.start();
        t3.start();
    }
}

This program demonstrates the usage of the join() method in Java, which is used to pause the execution of the current thread until the thread on which join() is called has finished its execution.

Step-by-Step Breakdown:

1. Class Declaration and Extending Thread:

 class TestJoinMethod1 extends Thread {

  •  The class TestJoinMethod1 is a subclass of the Thread class. This means that TestJoinMethod1 inherits all the properties and methods of the Thread class.
  • By overriding the run() method, we define the code that will execute when the thread starts.

2. Overriding the run() Method:

public void run()

{

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

    {

        try

        {

            Thread.sleep(500);  // Sleep for 500 milliseconds (half a second)

        }

        catch (Exception e)

        {

            System.out.println(e);

        }

        System.out.println(i);  // Print the current value of i

    }

}

  • The run() method has a loop with print statements printing numbers from 1 to 5 with a delay of 500 milliseconds between each print.
  • Thread.sleep(500) pauses the thread for half a second before printing each number.

3. Main Method – Creating Threads and Starting Them:

public static void main(String args[]) {

TestJoinMethod1 t1 = new TestJoinMethod1();  // Create a new thread t1

      TestJoinMethod1 t2 = new TestJoinMethod1();  // Create a new thread t2

      TestJoinMethod1 t3 = new TestJoinMethod1();  // Create a new thread t3

  • Three threads t1, t2, and t3 are started. Each thread is an object of the TestJoinMethod1 class.
  • All the threads will begin to call the method run() on the started state.

4. Thread t1 must be started. Now, calling join() on It;

t1.start();  // Starting the thread t1

try

{

    t1.join();  // Main thread waits for t1 to finish before continuing

}

catch (Exception e)

{

    System.out.println(e);

}

  • The start() method is called on t1. This creates the execution of the run() method in a different thread.
  • The join() method is invoked on t1. This is a call in the main that forces the waiting for t1 to complete execution of the run() method, so the method returns only then.

5. The threads t2 and t3 are launched.

t2. start ();  // Starting the thread t2

t3. start ();  // Starting the thread t3

During t1 termination as main program is waiting for it by calling the join() method, then t2 and t3 start

What the Threads Do?

  • The thread t1 will print from 1 to 5 with a delay of 500ms between numbers. The main program will wait for termination of t1 because of join().
  • The threads t2 and t3 are started only when t1 completes its execution because it invokes the join() method, so these print numbers from 1 to 5 with each print separated by 500 ms.

Order:

A. Thread t1 starts and begins its method run(). That prints 1, then a sleep of 500ms., prints 2, and does this until prints 5.

2. Once t1 is done, the main thread will start t2 and t3. Each of these threads will call its own run() method, printing numbers from 1 to 5 with the same delay.

Expected Output:

Because t1 will be running first due to the join(), t2 and t3 will only begin when t1 is finished; thus, both programs will print the numbers that each thread prints in the order of t1, t2, and t3. But since they’re running concurrently, the output may look like this:

1

2

3

4

5

1

2

3

4

5

1

2

3

4

5

Explanation of Output:

  • t1 prints numbers 1 through 5 first because the main thread waits for t1 to complete due to join().
  • Then the main thread starts t2 and t3 after t1 is over. Then both these threads in their turn print out numbers 1 through 5 in sequence but spaced out by a 500ms delay.

Note:

Hence, the output of t2 and t3 depends on concurrent execution of these threads, though numbers within a thread are always printed in sequence.

Conclusion:

  • join() will ensure that the main thread waits for t1 to complete before proceeding to execute t2 and t3.
  • The numbers 1 through 5 printed three times by each thread. Again, however, because of join() synchronizing, t1 just happens to print first.

Leave a Comment

Your email address will not be published. Required fields are marked *