How to use wait and notify concept in producer consumer situation

0 votes
How to use wait() and notify() concept in producer consumer situation?
Jul 20, 2018 in Java by Hannah
• 18,520 points
1,122 views

1 answer to this question.

0 votes

In this program, we have two threads named PRODUCER and CONSUMER, which are  instance of Producer and Consumer class respectively.  Both of these class implements Runnable interface. 

Main thread starts both PRODUCER and CONSUMER threads and also create an object of LinkedList class i.e. sharedQ to share as Queue between them.

maximum size of sharedQ is defined using maxSize variable.
 

public class ProducerConsumerTest {
    public static void main(String[] args) throws InterruptedException {    
     final Queue sharedQ = new LinkedList < Integer >();
    Thread consumerThread = new Thread(new Consumer(sharedQ, 4), "CONSUMER");
   Thread producerThread = new Thread(new Producer(sharedQ, 4), "PRODUCER");
    producerThread.start();
   consumerThread.start();
     }
}

Producer runs in an infinite loop and keeps inserting random integer value from 1 to 100 into sharedQ until the queue is full.

We use condition while(queue.size == maxSize) to confirm if queue is full or not. This condition is a part of synchronized block on shareQ object, so that no other thread can modify the queue while executing this line of code.

If sharedQ is full then our PRODUCER thread waits until CONSUMER thread consumes one item and makes space in queue.

It calls notify() method to inform PRODUCER thread. 

Both wait() and notify() methods are called on shared object which is sharedQ in our case.

class Producer implements Runnable
{
    private final Queue sharedQ;
    private int maxSize;   
    public Producer(Queue sharedQ, int maxSize)
    {
        this.sharedQ = sharedQ;
        this.maxSize = maxSize;
    }
         @Override
    public void run(){ 
        while(true)
        {
            synchronized (sharedQ) {
                while(sharedQ.size()==maxSize)
                {
                    try
                    {
                        System.out.println("Queue is full");
                        sharedQ.wait();
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                                     }
                Random random = new Random();
                int number = random.nextInt(100);
                System.out.println("Producing value " + number);
                sharedQ.add(number);
                sharedQ.notify();   
            }         
        }
    }
}

answered Jul 20, 2018 by Kalgi
• 52,350 points

Related Questions In Java

0 votes
2 answers

When and how to use Super() keyword in Java?

super() is used to call immediate parent. super() can be ...READ MORE

answered Jul 9, 2018 in Java by Sushmita
• 6,920 points
1,867 views
+2 votes
1 answer

How to import an existing x509 certificate and private key in Java keystore to use in SSL?

keytool does not provide such basic functionality ...READ MORE

answered Dec 19, 2018 in Java by Daisy
• 8,140 points
2,590 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,103 views
0 votes
2 answers

When to use LinkedList and ArrayList in Java?

ArrayList is what you want. LinkedList is almost always a ...READ MORE

answered Dec 11, 2018 in Java by Sushmita
• 6,920 points
1,140 views
0 votes
1 answer

How can we use wait and notify in Java?

You can refer to this: synchronized (someObject) { ...READ MORE

answered Jul 12, 2018 in Java by sophia
• 1,400 points
706 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,223 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,359 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,433 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,619 views
+12 votes
3 answers

How do I set or change the PATH system variable?

First make sure you have installed JDK ...READ MORE

answered Aug 28, 2018 in Java by slayer
• 29,370 points
1,416 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP