Code Monkey home page Code Monkey logo

producer-consumer-problem's Introduction

Producer-Consumer-Problem

Producer-Consumer-Problem

Producer-Consumer is a kind of design pattern where
a. Both producer and consumer can work at different spreed.
b. One doesn't need to know about the existance of other.
c. Separating producer and consumer results in a cleaner and more redable code.
d. Easy to manage both.

Psudeo code of producer and consumer problem with sempahore

Sempahore full = 0;
Sempahore empty = n;
Sempahore mutex = 1;

Producer()

void Producer() {
	int itemp;

	while(true) {
	produce(itemp);

	wait(empty);
	wait(mutex);

	Buffer[in] = temp;
	in = (in + 1) % n;

	signal(mutex);
	signal(full);
	}
}

Consumer()

void Consumer() {
	int itempc;
	
	while(true) {
		
	wait(full);
	wait(mutex);
	
	itempc = Buffer[out];
	out = (out + 1) % n;
	
	signal(mutex);
	signal(empty);
	
	consume(itempc);
	}
}

Java code

package com.demo;

class Sempahore {
    int s;

    public Sempahore(int s) {
        this.s = s;
    }

    public synchronized void waitS() {
        s--;
        if (s < 0)
            try {
                wait();
            } catch (InterruptedException e) {
                System.out.println("Exception in waitS");
                e.printStackTrace();
            }
    }

    public synchronized void singalS() {
        s++;
        if (s <= 0)
            notify();
    }
}

class BlockingQueue<T> {
    T[] array;
    int size;
    int in;
    int out;

    Sempahore mutex;
    Sempahore full;
    Sempahore empty;

    @SuppressWarnings("unchecked")
    public BlockingQueue(int size) {
        this.size = size;
        array = (T[]) new Object[size];
        mutex = new Sempahore(1);
        full = new Sempahore(0);
        empty = new Sempahore(size);
        in = 0;
        out = 0;
    }

    public void add(T item) {
        empty.waitS();
        mutex.waitS();

        array[in] = item;
        in = (in + 1) % size;

        full.singalS();
        mutex.singalS();
    }

    public T poll() {
        T item;

        full.waitS();
        mutex.waitS();

        item = array[out];
        out = (out + 1) % size;

        empty.singalS();
        mutex.singalS();
        return item;
    }
}

public class ProducerConsumerDemo {

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue<Integer> q = new BlockingQueue<>(5);

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    q.add(i);
                    System.out.println("Inserted: " + i);
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("Removed from thread 2: " + q.poll());
                }
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println("Removed from thread 3: " + q.poll());
                }
            }
        });

        t1.start();
        Thread.sleep(4000);
        t2.start();
        t2.join();
        t3.start();
        t1.join();
        t3.join();

        System.out.println("Terminated succesfully!");
    }
}

producer-consumer-problem's People

Contributors

upasana05ghosh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.