Producer consumer problem using threads in java


Producer consumer problem using threads in java

Week 7

 a) Aim: To Write a Java program that correctly implements producer consumer problem using the concept of inter thread communication.

Description: The producer-consumer problem (also known as the bounded-buffer problem) is a classic Java Example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer’s job is to generate a piece of data, put it into the buffer and start again. At the same time, the consumer is consuming the data (i.e., removing it from the buffer) one piece at a time. The problem is to make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.

Source code :

class Producer implements Runnable

{

            Q q;

            Producer(Q q)

            {

                        this.q=q;

                        new Thread(this,”producer”).start();

            }

            public void run()

            {

                        int i=0;

                        while(true)

                        {

                                    q.put(i++);

                                    if(i==5)

                                                System.exit(0);

                        }

            }

}

class Consumer implements Runnable

{

            Q q;

            Consumer(Q q)

            {

                        this.q=q;

                        new Thread(this,”consumer”).start();

            }

            public void run()

            {

                        while(true)

                                    q.get();

            }

}

class Program

{

            public static void main(String ar[])

            {

                        Q q=new Q();

                        new Producer(q);

                        new Consumer(q);

            }

}

class Q

{

            int n;

            boolean valueset=true;

            synchronized int get()

            {

                        while(!valueset)

                        {

                                    try

                                    {

                                                wait();

                                    }

                                    catch(Exception e)

                                    {

                                    }

                        }

                        System.out.println(“GET “+n);

                        valueset=false;

                        notify();

                        return n;

            }

            synchronized void put(int n)

            {

                        while(valueset)

                        {

                                    try

                                    {

                                                wait();

                                    }

                                    catch(Exception e)

                                    {

                                    }

                        }

                        this.n=n;

                        valueset=true;

                        System.out.println(“PUT “+n);

                        notify();          

            }

}

Sample output :

Get 0

Put 0

Get 1

Put 1

Get 2

Put 2

Producer consumer problem using threads in java

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories