Collections Class in Java

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.

Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread and is a daemon thread if and only if the creating thread is a daemon.

Thread class provides the join() method which allows one thread to wait until another thread completes its execution.

If there are multiple threads calling the join() methods that means overloading on join allows the programmer to specify a waiting period.

There are three overloaded join methods.

1). join()
It will put the current thread on wait until the thread on which it is called is dead. If the thread is interrupted then it will throw InterruptedException.

public final void join()

2). join(long millis)
It will put the current thread on wait until the thread on which it is called is dead or wait for a specified time (milliseconds).

public final synchronized void join(long millis)

3). join(long millis, int nanos)
It will put the current thread on wait until the thread on which it is called is dead or wait for a specified time (milliseconds + Nanos).

public final synchronized void join(long millis, int nanos)

public class ThreadJoinTest { public static void main(String[] args) { Thread t1 = new Thread(new MyRunnable(), "thread1"); Thread t2 = new Thread(new MyRunnable(), "thread2"); Thread t3 = new Thread(new MyRunnable(), "thread3"); Thread t4 = new Thread(new MyRunnable(), "thread4"); t1.start(); try { t1.join(2000); } catch(InterruptedException e) { e.printStackTrace(); } t2.start(); try { t1.join(); } catch(InterruptedException e) { e.printStackTrace(); } t3.start(); try { t2.join(); } catch(InterruptedException e) { e.printStackTrace(); } t4.start(); try { t1.join(); t2.join(); t3.join(); t4.join(); } catch(InterruptedException e) { System.out.println(e.getMessage()); } System.out.println("All threads are dead, exiting main"); } } class MyRunnable implements Runnable { @Override public void run() { System.out.println("The Thread is started : " + Thread.currentThread().getName()); try { Thread.sleep(4000); } catch(InterruptedException e) { System.out.println(e.getMessage()); } System.out.println("The Thread is ended : " + Thread.currentThread().getName()); } } output: The Thread is started : thread1 The Thread is started : thread2 The Thread is ended : thread1 The Thread is started : thread3 The Thread is ended : thread2 The Thread is started : thread4 The Thread is ended : thread3

Post/Questions related to  Thread Join Example in Java

How to create multiple threads in java?
How to check the currently running thread in java?
How to decide number of threads in java?
How to wait for a thread to finish java?
What is thread in Java?
What are the properties of thread in Java?
What is thread in java with example?
How do you control a thread in Java?
How do you start a thread in Java?
How to get current thread in java?

In this article, we have seen Thread Join Example in Java with Examples. All source code in the article can be found in the GitHub repository.