
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Concurrency Join Method
In this article, we will learn about concurrency in Java. It allows multiple threads to execute simultaneously, sharing system resources efficiently. One of the methods provided in the Thread class for managing thread execution is the join() method.
What is the join() Method?
the join () function is used to join the beginning of the execution of a thread to the end of the execution of another thread. This way, it is ensured that the first thread won't run until the second thread has stopped executing. This function waits for a specific number of milliseconds for the thread to terminate.
Syntax of join() function ?
public final void join()
Parameters ?
-
join(): Waits indefinitely for the thread to finish.
-
join(long millis): Waits for the specified time in milliseconds for the thread to finish.
- join(long millis, int nanos): Waits for the specified milliseconds and nanoseconds for the thread to complete.
How join() Works
When a thread calls myThread.join(), the following occurs ?
- The current thread pauses execution and waits for myThread to complete.
- The thread transitions from "Running" to "Waiting" state.
- Once myThread finishes, the waiting thread resumes execution.
Thread Execution with start() method ?
my_t.start(); // Starts a new thread executing the run() method.
Overriding the run() method of Runnable ?
public void run() { }
Creating a new thread using thread class and passing an instance of Demo to it ?
Thread my_t = new Thread(new Demo());
Making the main thread wait for my_t to finish for at most 30 milliseconds ?
my_t.join(30);
Example
Below is an example of the Java concurrency join () method ?
import java.lang.*; public class Demo implements Runnable{ public void run(){ Thread my_t = Thread.currentThread(); System.out.println("The name of the current thread is " + my_t.getName()); System.out.println("Is the current thread alive? " + my_t.isAlive()); } public static void main(String args[]) throws Exception{ Thread my_t = new Thread(new Demo()); System.out.println("The instance has been created and started"); my_t.start(); my_t.join(30); System.out.println("The threads will be joined after 30 milli seconds"); System.out.println("The name of the current thread is " + my_t.getName()); System.out.println("Is the current thread alive? " + my_t.isAlive()); } }
Output
The instance has been created and started The name of the current thread is Thread-0 Is the current thread alive? true The threads will be joined after 30 milliseconds The name of the current thread is Thread-0 Is the current thread alive? false
Time Complexity: O(n), where n is the number of iterations in the loops.
Space Complexity: O(1), as the program uses a constant amount of extra space regardless of input.
Conclusion
The join() method in Java ensures thread synchronization by making one thread wait until another finishes execution. It is useful in scenarios requiring sequential execution of threads. However, since it depends on the thread scheduler, its exact behavior may vary based on system configuration and CPU scheduling policies.