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?

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);
    }
}









Related Tutorials/Questions & Answers:
Please can I get the code for solution of producer consumer problem using semaphores?
please help me solve this problem when i am create database connection using servlecontext
Advertisements
Can find a solution or code for this - XML
please let me get code how to insert a data to mysql using setter and getter method by using java
please let me get code how to insert a data to mysql using setter and getter method by using java
I am having problem with configuring Hibernate sessionfactory. Please Help
ModuleNotFoundError: No module named 'Producer-Consumer'
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)) |
producer and concumer problem
Please tel me .In my System Oracle9i and J2sdk1.6 are Installed.Why this problem occures and what is the solution?
In JSP page, inside a javascript function, can I place a java code using scriptlet tag?
Can i write JavaScript code in AjaxResponse Code ?
i want java code for this xml file...please show me..
Begineer Help Help Please Thanks A million ) I am using Jcreator
Please find me a solution that
Please give me the code for the below problem - Java Interview Questions
Please tell me how can i convert string to timer
this is my javascript code and i am not understanding the mistake in this,please help me?
Please tell me where I can find tutorials on spring hibernate?
how to display jsp page containing mysql query in particular division using ajax ?my code is below bt i cundt get it properly
how can i write this program ?please suggest me
how i can get jqfade.js library
how can i simplify my java code
this is my code but i cant able to run it wt is the problem
this is my code but i cant able to run it wt is the problem
I need help on my Java code.... please please help me out!?
please help me to this problem..
this is my file upload page, here i am not getting tags input field value to my servlet,please suggest a solution?
can i know the error in this code... am unable to run this code
How can I get IBM certification for free?
How can I get into big data?
Can I get a job with just python?
Can I learn Python at 45 and get a job?
please any one can help me to write a code for this question?
How can I get the full URL with the attached parameters? - JSP-Servlet
please any one can help me to write a code for this question?
Can any one please help me in this ,,,,,,, need java code for this ,,,,please anyone
problem with code - Ajax
i want to create website,with manu with three level submenu on top side of page please send me code java
Can you see what i have done because i did it but i still have problem - SQL
I am getting error.How can i get details
error please send me the solution
error please send me the solution
Please provide the coding for this problem
Hibernate code problem - Hibernate
problem:struts code - Struts

Ads