Please can I get the code for solution of producer consumer problem using semaphores?

Ads
 

Please can I get the code for solution of producer consumer problem using semaphores?

Please can I get the code for solution of producer consumer problem using semaphores?

View Answers

August 19, 2011 at 4:11 PM

import java.util.*;
 class Semaphore{
     private int value;
     private int waiting;
     private String message;

    public Semaphore(int val, String mess){
    value = val;
    waiting = 0;
    message = mess;
    }
    public synchronized void down(){
    if (value > 0){
        value--;
        }
    else{
        try{
            waiting++;
            if (message != null)
                System.err.println(message);
            wait();
            }
        catch (InterruptedException e){}
        }
    }
    public synchronized void up(){
    if (waiting > 0){
        notify();
        waiting--;
        }
    else{
        value++;
        }
    }
}
public class ProducerConsumerUsingSemaphores{
    private Buffer b;
    private Semaphore full, empty, mutex;

    class Buffer{
    private LinkedList buf;
    private int items;
    private int size;
    public Buffer(int n){
        buf = new LinkedList();
        items = 0;
        size = n;
    }
    public Object remove(){
        Object result = buf.getFirst();
        buf.remove(0);
        items--;
        return result;
    }
    public void add(Object item){
        buf.add(item);
        items++;
    }
    }
    class Producer implements Runnable{
    private int itemsToAdd;
    public Producer(int n){
        itemsToAdd = n;
    }
    public void run(){
        int i = 0;
        while (true){
            try{
                Thread.sleep((int)(Math.random() * 10) * 100);
            }
            catch (InterruptedException e){}
            empty.down();
            mutex.down();
            b.add(new Integer(i));
            System.out.println("Producer added " + i);
            i++;
            mutex.up();
            full.up();
        }
    }
    }
    class Consumer extends Thread{
    private int id;
    public Consumer(int i){
        id = i;
    }
    public void run(){
        while (true){
            full.down();
            mutex.down();
            Integer item = (Integer)(b.remove());
            System.out.println("Consumer " + id + " removed " + item);
            mutex.up();
            empty.up();

            try{
                sleep((int)(Math.random() * 10) * 100);
            }
            catch (InterruptedException e){}
        }
    }
    }
    public ProducerConsumerUsingSemaphores(int items, int bufferSize, int consumers){
    b = new Buffer(bufferSize);
    empty = new Semaphore(bufferSize, "Producer sleeping");
    mutex = new Semaphore(1, null);
    full = new Semaphore(0, "Consumer sleeping");

    for (int id = 0; id < consumers; id++){
        Thread t = new Consumer(id);
        t.start();
        }

    new Thread(new Producer(items)).start();
    }
    public static void main(String[] args){
    int bufferSize =2;
    int consumers = 1;

    new ProducerConsumerUsingSemaphores(0, bufferSize, consumers);
    }
}

Ads









Related Tutorials/Questions & Answers:
Please can I get the code for solution of producer consumer problem using semaphores?
please let me get code how to insert a data to mysql using setter and getter method by using java
Advertisements
please let me get code how to insert a data to mysql using setter and getter method by using java
please help me solve this problem when i am create database connection using servlecontext
ModuleNotFoundError: No module named 'Producer-Consumer'
I am having problem with configuring Hibernate sessionfactory. Please Help
In JSP page, inside a javascript function, can I place a java code using scriptlet tag?
how i can get jqfade.js library
i have problem with this query... please tell me the resolution if this .........
i have problem with this query... please tell me the resolution if this .........
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |
StringIndexOutOfBoundException problem, What is mean in this code | first.charAt(i)) |
Can i write JavaScript code in AjaxResponse Code ?
How can I get IBM certification for free?
Can I get a job with just python?
Can I learn Python at 45 and get a job?
How can I get into big data?
Please tel me .In my System Oracle9i and J2sdk1.6 are Installed.Why this problem occures and what is the solution?
Please tell me how can i convert string to timer
how to display jsp page containing mysql query in particular division using ajax ?my code is below bt i cundt get it properly
I am getting error.How can i get details
please any one can help me to write a code for this question?
Please tell me where I can find tutorials on spring hibernate?
Begineer Help Help Please Thanks A million ) I am using Jcreator
how can i write this program ?please suggest me
i want java code for this xml file...please show me..
How can I generate diff image using Jmagick?
How can I get query string values in JavaScript?
Please find me a solution that
there are class A & class B.How can i get members of class A& B?

Ads