Java - Prductor Consumidor Usando Semaforors

 
Vista:

Prductor Consumidor Usando Semaforors

Publicado por Sargent (1 intervención) el 19/11/2007 21:14:07
mi problema es el siguiente, acabo de encontrar un programa en java que soluciona el problema del productor consumidor usando semáforos, el código es este...

Este es el programa principal

public class Producer_Consumer implements Runnable
{
static private Semaphore mutex = new Semaphore(1);
static private Semaphore full = new Semaphore(0);
static private Semaphore empty = new Semaphore(20);
static private int COUNT=0;
private String thread_name;
static int buffer[] = new int[20];
static private int current=0;

public Producer_Consumer (String name)
{
thread_name=name;
}

public void run ()
{
do
{
// am I a producer or a consumer?
int randy = (int)(Math.random() * 100);
if (((randy%2)==0) & (current>0)) // CONSUMER
{
full.WAIT();
mutex.WAIT();
// start of critical section
COUNT++;
current--;
System.out.println(thread_name +" removing buffer["+current +"]="+buffer[current] );
buffer[current]=-1;
// end of critical section
mutex.SIGNAL();
empty.SIGNAL();
try {
Thread.currentThread().sleep((int)(Math.random() * 100));}
catch
(InterruptedException e){}

}
else //PRODUCER
{
empty.WAIT();
mutex.WAIT();
// start of critical section
COUNT++;
buffer[current]=randy;
System.out.println(thread_name +" adding buffer["+current +"]="+buffer[current] );
current++;
// end of critical section
mutex.SIGNAL();
full.SIGNAL();
try {
Thread.currentThread().sleep((int)(Math.random() * 100));}
catch
(InterruptedException e){}

}
} while (COUNT<20);
}

public static void main (String args [])
{
// initialise buffer with some dud values
for(int i=0;i<20;i++) buffer[i]=-1;

// create two producer/consumer threads
Thread T1 = new Thread(new Producer_Consumer("T1"));
Thread T2 = new Thread(new Producer_Consumer("T2"));

// and start them off
T1.start();
T2.start();
}

}

y esta es la clase semáforo

class Semaphore {
private int count;
public Semaphore(int n) {
this.count=n;
}
public synchronized void WAIT(){
while(count==0){
try{wait();}
catch (InterruptedException e){
//keep trying
}
}
count--;
}
public synchronized void SIGNAL(){
count++;
notify();//alert a thread that's blocking the semaphore
}
}

Lo que quiero sabes es si hay alguien que pueda explicarme más o menos que es lo que hace este programa y como es el manejo del buffer, ósea un pequeño resumen de lo que hace el programa en si.......es que lo analizo miles de veces, pero cada vez que lo hago saco conclusiones diferentes...

Gracias y saludos...
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Prductor Consumidor Usando Semaforors

Publicado por tata (1 intervención) el 01/07/2008 01:52:01
Porque mejor no estudias , antes de estar buscando los programas resueltos y que otros te los resuelvan.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Prductor Consumidor Usando Semaforors

Publicado por ana (1 intervención) el 14/02/2014 07:15:03
animal que ni que el código estuviera tan sencillo asta los expertos buscan en Internet !
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Prductor Consumidor Usando Semaforors

Publicado por markiiki (1 intervención) el 17/09/2014 22:24:21
Pues si estoy de acuerdo hasta los expertos buscan en internet, pero a lo mejor debería trabajar mas en la lógica para poder entender el programa
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar