Java — Inter Thread Communication : wait(), notify() and notifyAll() | Code Factory

Index Page : Link

Donate : Link

WordPress Blog : Link

  • 2 Threads can communicate with each other by wait(), notify() and notifyAll() methods.
Method           Is thread release lock?
yield() -> No
join() -> No
sleep() -> No
wait() -> Yes
notify() -> Yes
notifyAll() -> Yes

Which of the following is valid :

1. If a thread calls wait() method immediately it will entered into waiting state without releasing any lock?

  • Invalid

2. If a thread calls wait() method it release the lock of that object but may not immediately.

  • Invalid

3. If a thread calls wait() method on any object it release all locks acquired by that thread and immediately entered into waiting state.

  • Invalid

4. If a thread calls wait() method on any object ot immediately release lock of that particular object and entered into waiting state.

  • Valid

5. If a thread calls notify() method on any object it immediately release lock of that particular object.

  • Invalid

6. If a thread calls notify() method on any object it release the lock of that object but may not immediately.

  • Valid
/*
Reference : https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html
*/
/* Causes the cur+rent thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0). */
public final void wait() throws InterruptedException
/* Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. */
public final void wait(long timeout) throws InterruptedException
/* Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed. */
public final void wait(long timeout, int nanos) throws InterruptedException
/* Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods. */
public final void notify()
/* Wakes up all threads that are waiting on this object's monitor. A thread waits on an object's monitor by calling one of the wait methods. */
public final void notifyAll()
  • Every wait() method throws InterruptedException which is checked exception. Hence whenever we are using wait() method compulsary we should handle this IE either by try catch or throws keyword otherwise we will get compile time error.
package com.example.thread;/**
* @author code.factory
*
*/
public class ThreadTest {
public static void main(String... args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
synchronized (t) {
System.out.println("main thread calling wait() method");
t.wait();
System.out.println("main thread got notification");
System.out.println(t.total);
}
}
}
class MyThread extends Thread {
int total = 0;
public void run() {
synchronized (this) {
System.out.println("child thread start calculation");
for(int i=0; i<5; i++) {
total = total + i;
}
System.out.println("child thread giving notification");
this.notify();
}
}
}

Output :

main thread calling wait() method
child thread start calculation
child thread giving notification
main thread got notification
10

Producer Consumer Problem :

  • Producer thread is responsible to produce items to the queue and consumer thread is responsible to consume items from the queue.

Difference Between notidy() and notifyAll() :

  • We can use notify() method to give the notification for only one waiting thread, If multiple threads are waiting then only one thread will be notify and the remaining threads have to wait for further notifications.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store