How to make a Java thread wait for another thread's output?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In concurrent programming using Java, there might be scenarios where you need one thread to wait for the output of another thread before it continues execution. This is particularly common in complex applications where tasks are interdependent. This article will delve into the methods available in Java to make one thread wait for another's completion, providing technical explanations and examples along the way.
Java Thread Basics
Before diving into inter-thread communication, it is important to understand some basics of Java threads:
- Thread: A thread in Java is the smallest unit of processing. Threads can run concurrently.
- Lifecycle: A thread goes through different states such as New, Runnable, Blocked, Waiting, Timed Waiting, and Terminated.
Techniques for Inter-Thread Communication
There are several ways to make one thread wait for the output of another thread in Java, which include:
- Thread Join
- Using Shared Objects with Wait and Notify
- CountDownLatch
We'll look into each of these methods with examples to illustrate how they function.
1. Thread Join
The join() method is a simple and effective way to make one thread wait for the completion of another. The join() method can be called on a thread to ensure that the current thread waits until the thread it is called upon completes.
Example:
2. Using Shared Objects with Wait and Notify
The wait() and notify() methods are part of the Object class and are used for inter-thread communication by synchronizing on a shared object.
Example:
3. CountDownLatch
CountDownLatch is a synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.
Example:
Summary of Key Methods
| Method/Concept | Description | Use Case |
join() | Waits for a thread to die | One thread waiting for another's completion |
wait()/notify() | Communication through a shared object | Complex inter-thread communication |
CountDownLatch | Synchronization aid to wait for operations | Multiple threads completing tasks independently |
Conclusion
Knowing how to effectively synchronize threads in Java is essential for writing efficient and correct parallel applications. The tools provided by the Java platform, such as join(), wait()/notify(), and CountDownLatch, provide powerful ways to manage inter-thread dependencies and ensure the controlled execution of concurrent tasks. By understanding how and when to use these mechanisms, you can ensure that your programs are both robust and highly efficient.
Related reading
- how to make an application thread safe?
- How to make async messaging fast and reliable in a sync environment?
- How to make asynchronous tasks have lower priority in Java 8?
- How to make different calls to an async method waiting for the result of first call?
- How to make a long polling with Quarkus
- How to make a new List in Java
- How to make HTTP requests in PHP and not wait on the response
- How to make main thread wait for all child threads finish?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.