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 *

Domain & free Hosting

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.