Joining Threads in Java
The join() method of the Thread class enables one thread to wait until another thread has finished running. If t is a Thread object that is presently running, t.join() will ensure that t is ended before the program executes the next instruction. Until the thread on which it is called is dead, the current thread will be placed on hold by the join() function.
Syntax:
Public final void join() is the syntax.
Join(long millis): This command will put the running thread on hold until the thread on which it is called has terminated or until the millisecond timeout has expired.
Syntax:
Public final synchronized void join(long millis) is the syntax.
Until the thread on which it is called is dead, the current thread will be placed on hold by the join() function.
When a thread is interrupted, InterruptedException is thrown. Join(long millis): This command will put the running thread on hold until the thread on which it is called has terminated or until the millisecond timeout has expired.
Example:
import java.util.logging.Logger; public class FirstCode extends Thread { private static final Logger LOGGER = Logger.getLogger(FirstCode.class.getName()); public int processingCount = 0; FirstCode(int processingCount) { this.processingCount = processingCount; LOGGER.info("Thread Created"); } @Override public void run() { LOGGER.info("Thread " + this.getName() + " started"); while (processingCount > 0) { try { Thread.sleep(1000); } catch (InterruptedException e) { LOGGER.info("Thread " + this.getName() + " interrupted in FirstCode"); } processingCount--; LOGGER.info("Inside Thread " + this.getName() + ", processingCount = " + processingCount); } LOGGER.info("Thread " + this.getName() + " exiting in FirstCode"); } public static void main(String[] args) throws InterruptedException { FirstCode t2 = new FirstCode(1); t2.start(); LOGGER.info("Invoking join"); t2.join(); LOGGER.info("Returned from join"); LOGGER.info("Is t2 alive? " + t2.isAlive()); } }
Output
Dec 02, 2023 4:43:11 AM FirstCode <init>
INFO: Thread Created
Dec 02, 2023 4:43:11 AM FirstCode main
INFO: Invoking join
Dec 02, 2023 4:43:11 AM FirstCode run
INFO: Thread Thread-0 started
Dec 02, 2023 4:43:12 AM FirstCode run
INFO: Inside Thread Thread-0, processingCount = 0
Dec 02, 2023 4:43:12 AM FirstCode run
INFO: Thread Thread-0 exiting in FirstCode
Dec 02, 2023 4:43:12 AM FirstCode main
INFO: Returned from join
Dec 02, 2023 4:43:12 AM FirstCode main
INFO: Is t2 alive? false
The current thread calls the method “join()” on a another thread, forcing the latter to block until the first one finishes. The current thread calls the method join(long millisec) on a second thread, which causes the current thread to stall until the second thread ends or the predetermined amount of milliseconds has elapsed.
The current thread invokes the method join(long millisec, int nanos) on a second thread, causing the current thread to block until the second thread exits or the specified number of milliseconds plus nanoseconds has passed.
Importance of JOIN() Method:
A thread cannot begin executing until another thread has finished, hence the join() final function of the Thread class can be used to link the beginning of one thread’s execution to the end of another thread’s execution.
The thread that is presently active will pause if the join() method is used on a thread instance before the thread instance has finished running. Timeout for Thread.join() Methods. If the referenced thread is blocked or takes too long to process, the join() method will continue to wait. This can cause problems because the calling thread will become unresponsive. To handle these cases, we use overloaded join() methods that allow us to specify a timeout period.
Conclusion
To make sure a thread in Java finishes running before the program moves on to the next phase, use the join() method. The calling thread waits until the specified thread terminates when it calls join() on another thread. To coordinate the flow of execution in multi-threaded applications and enable one thread to wait for the completion of another, synchronization is necessary. By ensuring that the program acts as intended and assisting in the prevention of race problems, it offers a mechanism to control the sequence of execution in a concurrent context.
In conclusion, Java multi-threading relies heavily on the join() function to preserve a logical order of operations.