Java Multithreading: Wait and Notify (Video Tutorial Part 8)

A tutorial on wait and notify in Java; low-level multithreading methods of the Object class that allow you to have one or more threads sleeping, only to be woken up by other threads at the right moment. Extremely useful for avoiding those processor-consuming polling loops.

After starting the video, click the maximise button to make it fullscreen so you can see the code!



Code For This Tutorial



The main program just runs the produce() and consume() methods in different threads.

 
import java.util.Scanner;


public class Processor {

    public void produce() throws InterruptedException {
        synchronized (this) {
            System.out.println("Producer thread running ....");
            wait();
            System.out.println("Resumed.");
        }
    }

    public void consume() throws InterruptedException {
        
        Scanner scanner = new Scanner(System.in);
        Thread.sleep(2000);
        
        synchronized (this) {
            System.out.println("Waiting for return key.");
            scanner.nextLine();
            System.out.println("Return key pressed.");
            notify();
            Thread.sleep(5000);
        }
    }
}

 
Producer thread running ....
Waiting for return key.

Return key pressed.
Resumed.





The main program (just creates and runs two threads):

 

public class App {

    public static void main(String[] args) throws InterruptedException {

        final Processor processor = new Processor();

        Thread t1 = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    processor.produce();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    processor.consume();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        
        t1.start();
        t2.start();
        
        t1.join();
        t2.join();
    }
}