How solve consumer/producer task using semaphores

Posted by user1074896 on Stack Overflow See other posts from Stack Overflow or by user1074896
Published on 2012-03-28T11:16:08Z Indexed on 2012/03/28 11:29 UTC
Read the original article Hit count: 169

Filed under:
|

I have SimpleProducerConsumer class that illustrate consumer/producer problem (I am not sure that it's correct).

public class SimpleProducerConsumer {
    private Stack<Object> stack = new Stack<Object>();
    private static final int STACK_MAX_SIZE = 10;

    public static void main(String[] args) {
        SimpleProducerConsumer pc = new SimpleProducerConsumer();
        new Thread(pc.new Producer(), "p1").start();
        new Thread(pc.new Producer(), "p2").start();
        new Thread(pc.new Consumer(), "c1").start();
        new Thread(pc.new Consumer(), "c2").start();
        new Thread(pc.new Consumer(), "c3").start();
    }

    public synchronized void push(Object d) {
        while (stack.size() >= STACK_MAX_SIZE)
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        stack.push(new Object());
        System.out.println("push " + Thread.currentThread().getName() + "    " + stack.size());
        notify();
    }

    public synchronized Object pop() {
        while (stack.size() == 0)
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        stack.pop();
        System.out.println("pop  " + Thread.currentThread().getName() + "    " + stack.size());
        notify();
        return null;
    }

    class Consumer implements Runnable {
        @Override
        public void run() {
            while (true) {
                pop();
            }
        }
    }

    class Producer implements Runnable {
        @Override
        public void run() {
            while (true) {
                push(new Object());
            }
        }
    }
}

I found simple realization of semaphore(here:http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html I know that there is concurrency package) How I need to change code to exchange java objects monitors to my custom semaphore. (To illustrate C/P problem using semaphores)

Semaphore:

class Semaphore {
    private int counter;

    public Semaphore() {
        this(0);
    }

    public Semaphore(int i) {
        if (i < 0)
            throw new IllegalArgumentException(i + " < 0");
        counter = i;
    }

    public synchronized void release() {
        if (counter == 0) {
            notify();
        }
        counter++;
    }

    public synchronized void acquire() throws InterruptedException {
        while (counter == 0) {
            wait();
        }
        counter--;
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading