Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – join() Method


Java Programming Language join Method in Multithreading

Using join() method, we make one thread to wait until another thread complete its work.

Assume there are two threads A and B. If we want that thread A should wait till thread B gets complete, then we should use join() method on B. That is thread A should call B.join().

Once thread A call B.join()A enters into waiting state and thread B starts its execution until it gets complete. A starts its execution after completion of B.

Syntax of join() method

public final void join()

public final void join(long milliSeconds)

public final void join(long milliSeconds, int nanoSeconds)

We can also make one thread to wait only for specific time and allow another thread to execute only for that specific time by using last two join methods specified above.

Second join method specifies the time in millisecond

It means, the thread which calls join(milliSecondsmethod waits for those specific milliseconds till another thread on which join(milliSeconds) is called, executes for that time.

And the third method specifies the time in milliseconds plus nanoseconds

It means, the thread which calls join(milliSeconds, nanoSeconds) method waits for that specific nanoseconds+milliseconds till another thread on which join(milliSeconds, nanoSeconds) is called, executes for that time.

Example using, public final void join() method

class ChildThread extends Thread
{
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println("Child Thread");
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException e)
            {
                System.out.println("Exception caught");
            }
        }
    }
}

class JoinDemo
{
    public static void main(String args[])
    {
        ChildThread t = new ChildThread();
        t.start();
        try
        {
            t.join();
        }
        catch(InterruptedException e)
        {
            System.out.println("Exception Caught");
        }
        
        for(int i=1;i<=5;i++)
        {
            System.out.println("Main thread");
        }
    }
}

Output

Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main thread
Main thread
Main thread
Main thread
Main thread

Here main thread calls join() method on thread t. Therefore main thread waits till Child thread executes its for loop continuously (with each iteration after every 100 milliseconds), and then main thread executes its for loop continuously.

The join() method may throw checked exception InterruptedException; therefore it should be surrounded by a try-catch clause that can catch InterruptedException or should use throws clause to declare InterruptedException at main method header.

Example using, join(long millisecond) method

class ChildThread extends Thread
{
    public void run()
    {
        for(int i=1;i<=5;i++)
        {
            System.out.println("Child Thread");
            try
            {
                Thread.sleep(100);
            }
            catch(InterruptedException e)
            {
                System.out.println("Exception caught");
            }
        }
    }
}

class JoinDemo
{
    public static void main(String args[])
    {
        ChildThread t = new ChildThread();
        t.start();
        try
        {
            t.join(200);
        }
        catch(InterruptedException e)
        {
            System.out.println("Exception Caught");
        }
        for(int i=1;i<=5;i++)
        {
            System.out.println("Main thread");
        }
    }
}

Output

Child Thread
Child Thread
Main thread
Main thread
Main thread
Main thread
Main thread
Child Thread
Child Thread
Child Thread

Here main thread waits only for 200 milliseconds to execute Child thread.

 

Leave a reply