Producer Consumer & Observer Design Pattern

The Producer Consumer Problem often stated in the examples of Multi Threading is by far the most intriguing for a beginner stepping into the threading and concurrent environment. How two threads work in a synchronized manner to achieve a common or somewhat to the same path GOAL is not just fascinating but also illustrative enough for someone to learn the very basics of multi threaded programming. For more read this.

The Observer Design Pattern is a simple yet powerful mechanism to transfer information from one source to many recipients whenever state of the source changes. For more read this

StackAsWeKnowIt is a simple application which with the help of a simple animation depicts the usage of a fixed size stack.

I have used Producer Consumer mechanism of Threads and have utilized the Observer Design Pattern to achieve the required animation.

To elaborate, ProducerConsumer Object repeatedly pushes and pulls values to the Stack while also updating the Applet about the change in the stack size, this is done using the Observer Design Pattern, which in turn runs a simple animation which portrays how stack is filled and emptied continuosly.

Let’s take a brief look at the code involved …

    public void produce() {
        while (true) {
            synchronized (stack) {
                try {
                    if(stack.getSize() == stack.getBound())
                        stack.wait();
                    stack.push(random.nextInt());
                    notifyObservers();
                    stack.notify();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void consume() {
        while (true) {
            synchronized (stack) {
                try {
                    if(stack.getSize() == -1)
                        stack.wait();
                    stack.pull();
                    notifyObservers();
                    stack.notify();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public class Stack {

        private Integer[]    container;
        private int            top;

        public Stack(int size) {
            container = new Integer[size];
            top = -1;
        }

        public Stack() {
            container = new Integer[10];
            top = -1;
        }

        public void push(Integer value) {
            if(top < container.length) {
                System.out.println(&amp;quot;pushed &amp;quot; + (top + 1));
                container[++top] = value;
            } else {
                throw new ArrayIndexOutOfBoundsException();
            }
        }

        public Integer pull() {
            if(top > -1) {
                System.out.println(&amp;quot;pulled &amp;quot; + top);
                return container[top--];
            } else {
                throw new ArrayIndexOutOfBoundsException();
            }
        }
    }

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑