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...
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


0