java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.join() will make sure that t is terminated before the next instruction is executed by the program.
If there are multiple threads calling the join() methods that means overloading on join allows the programmer to specify a waiting period.
However, as with sleep, join is dependent on the OS for timing, so you should not assume that join will wait exactly as long as you specify. There are three overloaded join functions.
1. join(): It will put the current thread on wait until the thread on which it is called is dead. If a thread is interrupted then it will throw InterruptedException.
Syntax:
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 the specified time (milliseconds).
Syntax:
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 the specified time (milliseconds + nanos).
Syntax:
public final synchronized void join(long millis, int nanos)
Java
// Java program to explain the
// concept of joining a thread.
import java.io.*;
// Creating thread by creating the
// objects of that class
class ThreadJoining extends Thread
{
@Override
public void run()
{
for (int i = 0; i < 2; i++)
{
try
{
Thread.sleep(500);
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
}
catch(Exception ex)
{
System.out.println("Exception has" +
" been caught" + ex);
}
System.out.println(i);
}
}
}
class GFG
{
public static void main (String[] args)
{
// creating two threads
ThreadJoining t1 = new ThreadJoining();
ThreadJoining t2 = new ThreadJoining();
ThreadJoining t3 = new ThreadJoining();
// thread t1 starts
t1.start();
// starts second thread after when
// first thread t1 has died.
try
{
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
t1.join();
}
catch(Exception ex)
{
System.out.println("Exception has " +
"been caught" + ex);
}
// t2 starts
t2.start();
// starts t3 after when thread t2 has died.
try
{
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
t2.join();
}
catch(Exception ex)
{
System.out.println("Exception has been" +
" caught" + ex);
}
// t3 starts
t3.start();
// After t2 has dead, t3 starts
try
{
System.out.println("Current Thread: "
+ Thread.currentThread().getName());
t3.join();
}
catch(Exception ex)
{
System.out.println("Exception has been" +
" caught" + ex);
}
}
}
// This code is modified by Susobhan Akhuli
Output
Current Thread: main
Current Thread: Thread-0
0
Current Thread: Thread-0
1
Current Thread: main
Current Thread: Thread-1
0
Current Thread: Thread-1
1
Current Thread: main
Current Thread: Thread-2
0
Current Thread: Thread-2
1
In the above example we can see clearly second thread t2 starts after first thread t1 has died and t3 will start its execution after second thread t2 has died.
Similar Reads
Multithreading in Java Multithreading is a Java feature that allows the concurrent execution of two or more parts of a program for maximum utilization of the CPU. Each part of such a program is called a thread. So, threads are lightweight processes within a process.Different Ways to Create ThreadsThreads can be created by
3 min read
Lifecycle and States of a Thread in Java A thread in Java can exist in any one of the following states at any given time. A thread lies only in one of the shown states at any instant:New StateRunnable StateBlocked StateWaiting StateTimed Waiting StateTerminated StateThe diagram below represents various states of a thread at any instant:Lif
5 min read
Main thread in Java Java provides built-in support for multithreaded programming. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.When a Java program starts up, one thread begins running i
4 min read
Java Concurrency - yield(), sleep() and join() Methods In this article, we will learn what is yield(), join(), and sleep() methods in Java and what is the basic difference between these three. First, we will see the basic introduction of all these three methods, and then we compare these three. We can prevent the execution of a thread by using one of th
5 min read
Inter-thread Communication in Java Inter-thread communication in Java is a mechanism in which a thread is paused from running in its critical section, and another thread is allowed to enter (or lock) the same critical section to be executed.Note: Inter-thread communication is also known as Cooperation in Java.What is Polling, and Wha
6 min read
Java Thread Class Thread is a line of execution within a program. Each program can have multiple associated threads. Each thread has a priority which is used by the thread scheduler to determine which thread must run first. Java provides a thread class that has various method calls to manage the behavior of threads b
5 min read
What does start() function do in multithreading in Java? We have discussed that Java threads are typically created using one of the two methods : (1) Extending thread class. (2) Implementing RunnableIn both the approaches, we override the run() function, but we start a thread by calling the start() function. So why don't we directly call the overridden ru
2 min read
Java Thread Priority in Multithreading Java being Object-Oriented works within a Multithreading environment in which the thread scheduler assigns the processor to a thread based on the priority of the thread. Whenever we create a thread in Java, it always has some priority assigned to it. Priority can either be given by JVM while creatin
5 min read
Joining Threads in Java java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.join() will make sure that t is terminated before the next instruction is executed by the program. If th
3 min read
Java Naming a Thread and Fetching Name of Current Thread A thread can be referred to as a lightweight process. Assigning descriptive names to threads enhances code readability and simplifies debugging. Now let us discuss the different ways to name a thread in Java.Methods to Set the Thread NameThere are two ways by which we can set the name either be it d
4 min read