What is inter-thread communication in Java
๐ก Concept: Inter-thread communication
Inter-thread communication allows threads to cooperate by waiting and notifying each other for coordinated execution.
๐ Quick Intro
Threads use wait()
, notify()
, and notifyAll()
methods on shared objects to communicate and avoid race conditions.
๐ง Analogy
Think of inter-thread communication like a conversation where one thread waits for a signal to proceed and another thread sends that signal.
๐ง Technical Explanation
wait()
causes the current thread to wait until notified.notify()
wakes up one waiting thread.notifyAll()
wakes up all waiting threads.- These methods must be called from synchronized context.
- Used to coordinate threads sharing resources.
๐ฏ Use Cases
- โ Producer-consumer problem solution.
- โ Thread coordination in multi-threaded apps.
- โ Avoid busy waiting and CPU wastage.
๐ป Inter-thread Communication Example
class Message {
private String msg;
public synchronized void send(String m) {
this.msg = m;
notify();
}
public synchronized void receive() {
try {
wait();
System.out.println("Received: " + msg);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

โ Interview Q&A
Q1: What is inter-thread communication?
A: Coordination between threads using wait, notify, notifyAll.
Q2: What does wait() do?
A: Makes the thread wait until notified.
Q3: What is the difference between notify() and notifyAll()?
A: notify() wakes one thread, notifyAll() wakes all.
Q4: Can wait(), notify() be called outside synchronized block?
A: No, causes IllegalMonitorStateException.
Q5: What is producer-consumer problem?
A: Threads producing and consuming shared resources.
Q6: How to avoid busy waiting?
A: Using wait/notify instead of loops.
Q7: What happens if notify() is called with no waiting threads?
A: No effect.
Q8: What exceptions can wait() throw?
A: InterruptedException.
Q9: Can notifyAll() cause performance issues?
A: Yes, waking all threads may cause overhead.
Q10: How to safely use wait/notify?
A: Always inside synchronized blocks.
๐ MCQs
Q1. What is inter-thread communication?
- Parallel execution
- Coordination between threads
- Race condition
- Deadlock
Q2. What does wait() do?
- Starts thread
- Makes thread wait until notified
- Stops thread
- Kills thread
Q3. Difference between notify() and notifyAll()?
- Both wake one
- notify() wakes one, notifyAll() all
- Both wake all
- No difference
Q4. Can wait() be called outside synchronized?
- Yes
- No
- Sometimes
- Only in Java 9+
Q5. What is producer-consumer problem?
- Single thread
- Threads producing and consuming
- Deadlock
- Race condition
Q6. How to avoid busy waiting?
- Using loops
- Using sleep()
- Using wait/notify
- Ignoring
Q7. What happens if notify() called with no waiting threads?
- Exception
- No effect
- Crash
- Deadlock
Q8. What exceptions can wait() throw?
- IOException
- InterruptedException
- RuntimeException
- Error
Q9. Can notifyAll() cause performance issues?
- No
- Yes
- Sometimes
- Never
Q10. How to safely use wait/notify?
- Outside synchronized
- Inside synchronized blocks
- Anywhere
- Only in static methods
๐ก Bonus Insight
Inter-thread communication is essential for effective thread coordination and resource sharing in Java concurrency.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!