Most of you have heard about the Producer-Consumer problem where one is producing something and another is consuming it.
So basically what actually the producer-consumer problem says
Producer-Consumer Problem says that if we have a shared resource among 2 threads, and producer's job is to generate the data and put it in the buffer and consumer's job is to take the data from the buffer but then where is the problem? So problem is there should not be a scenario where the producer has produced the data more than the buffer size, which results in overflow problem also consumer should not consume all the data in the buffer and again try, which result in underflow problem.
So basically its a multi-thread synchronization problem.
Read about: Concurrency CountDownLatch in java
Read about: Concurrency CountDownLatch in java
Now, what is the solution?
We can use wait-notify mechanism to solve this problem of synchronization.
Approach
- We will have 2 thread one is to produce random data and second will get the random data from the shared LinkedList (or and queue).
- we will write on a class called processor where there will be 2 methods one is produce() and second will be consume ()
- In produce() method we will write random generator and also be checking whether the size of linked list is less than 10 or not if greater we will call wait method over the object called lock which we are using for the locking mechanism.
- when produce() put the data in linkedlist it will call lock.notify() which will notify the second thread that there are some data stored in the linked list.
- now in consume() we will have a check for the linked list size should not be 0 if so we will call lock.wait() else we will take the data from the list and notify the producer().
#Implemenation
Processor.java (this class will have the produce() and consume() method in it )
class Processor { LinkedList<Integer> list = new LinkedList<>(); Object lock = new Object(); int value =0; public void produce() throws InterruptedException{ while(true) { synchronized (lock) { while(list.size() == 10) lock.wait(); list.add(value++); lock.notify(); } } } public void consume() throws InterruptedException { Random random = new Random(); while(true){ synchronized (lock) { while(list.size() == 0) lock.wait(); int i =list.removeFirst(); lock.notify(); System.out.println("Got the value "+i + "now the list size is "+list.size()); } Thread.sleep(random.nextInt(1000)); } } }
Now we will see the 2 thread class and how we are calling them.
ProducerConsumerWithWaitNotify.Java
ProducerConsumerWithWaitNotify.Java
public class ProducerConsumerWithWaitNotify { public static void main(String[] args) { Processor pro = new Processor(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { pro.produce(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { try { pro.consume(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t1.start(); t2.start(); } }
After executing the code what will be the result, It will be like below
Got the value 0now the list size is 9 Got the value 1now the list size is 9 Got the value 2now the list size is 9 Got the value 3now the list size is 9 Got the value 4now the list size is 9 Got the value 5now the list size is 9 Got the value 6now the list size is 9 Got the value 7now the list size is 9 Got the value 8now the list size is 9 Got the value 9now the list size is 9 Got the value 10now the list size is 9 Got the value 11now the list size is 9 Got the value 12now the list size is 9 Got the value 13now the list size is 9 Got the value 14now the list size is 9
I hope this will help you in understanding the producer-consumer problem implementation using wait notify method in java
If you have any issue or concern, please leave us a comment, will be happy to help
Thanks for reading
noeik