Producer Consumer problem in java using wait and notify | multithreading and blocking queue with bounded waiting

Blocking queue: Achieved using List

 

 

Producer:

public void produce() throws InterruptedException {    int value = 0;    synchronized(this) {       while (list.size() == capacity)             wait();       System.out.println("Producer produced-" + value);       list.add(value++);       notify();    }  }

Consumer:

public void consume() throws InterruptedException {    synchronized(this) {         while (list.size() == 0)                 wait();                  int val = list.removeFirst();         System.out.println("Consumer consumed-" + val);         notify();}

Adding Producer and Consumer in One Class :