Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Implement Producer Consumer Problem Using Threading in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 753
    Comment on it

    ProducerConsumer problem is also known as the bounded-buffer problem.It is multi-process synchronization problem.There are two processes one is producer and another is consumer that can share a common and fixed-size buffer. The producer insert the data into the buffer . At same point of time, consumer is consuming the data. The problem is ensure that the producer not insert the data if buffer is full and consumer not getting data if buffer is empty.


    In below example we have used two classes Producer and Consumer they can share common resource from Resource class. we have used put and get method from Resource class and make both of them synchronized means only single thread is entered at a time in method. If Producer can put the element using put method they not put another element till the first element is not getting by the get method from the Consumer.

    import java.io.*;
    
    class Resource
    {
    int n;
    int flag=0;
        synchronized public void get()throws Exception
        {
            if(flag==0)
            {
                wait();
            }
            else
            {
                System.out.println("Get:="+n);
                notify();
                flag=0;
            }   
    
        }
        synchronized public void put(int a)throws Exception
        {
            flag=1;
            this.n=a;
            System.out.println("Put:="+n);
            notify();
            wait();
    
        }
    }
    
    class Producer implements Runnable
    {
    Resource r;
        Producer(Resource c)
        {
            r=c;
            Thread t1=new Thread(this,"Producer");
            t1.start();
        }
        public void run()
        {
            int i=0;
            try{
                while(i<10)
                {
                    i++;
                    r.put(i);
                }
            }
            catch(Exception e){}
        }
    
    }
    
    class Consumer implements Runnable
    {
    Resource r;
        Consumer(Resource c)
        {
            r=c;
            Thread t=new Thread(this,"Consumer");
            t.start();
        }
        public void run()
        {
            try{
                while(true)
                {
                    r.get();
                    //System.out.println(r.get());
                }
            }
            catch(Exception e){}
        }
    }
    
    class Main
    {
        public static void main(String...s)
        {
            Resource r=new Resource();
            Producer p=new Producer(r);
            Consumer c=new Consumer(r);
    
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: