You are on page 1of 9

CONSUMIDOR

/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package productorconsumidor;
/**
* Esta clase representa al consumidor
* @author francis
*/
public class Consumidor implements Runnable
{
private final Contenedor contenedor;
private final int idconsumidor;
/**
* Constructor de la clase
* @param contenedor Contenedor comn a los
consumidores y el productor
* @param idconsumidor Identificador del
consumidor
*/
public Consumidor(Contenedor contenedor, int
idconsumidor)

{
this.contenedor = contenedor;
this.idconsumidor = idconsumidor;
}
@Override
/**
* Implementacin de la hebra
*/
public void run()
{
while(Boolean.TRUE)
{
System.out.println("El consumidor " +
idconsumidor + " consume: " + contenedor.get());
}
}
}
_______________________
CONTENEDOR

/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/

package productorconsumidor;
/**
* Esta clase representa el contenedor donde se
producirn los elementos que consumen los
consumidores
* @author francis
*/
public class Contenedor
{
private int contenido;
private boolean contenedorlleno =
Boolean.FALSE;
/**
* Obtiene de forma concurrente o sncrona el
elemento que hay en el contenedor
* @return Contenido el contenedor
*/
public synchronized int get()
{
while (!contenedorlleno)
{
try
{
wait();
}
catch (InterruptedException e)

{
System.err.println("Contenedor: Error en
get -> " + e.getMessage());
}
}
contenedorlleno = Boolean.FALSE;
notify();
return contenido;
}
/**
* Introduce de forma concurrente o sncrona un
elemento en el contenedor
* @param value Elemento a introducir en el
contenedor
*/
public synchronized void put(int value)
{
while (contenedorlleno)
{
try
{
wait();
}
catch (InterruptedException e)
{
System.err.println("Contenedor: Error en
put -> " + e.getMessage());

}
}
contenido = value;
contenedorlleno = Boolean.TRUE;
notify();
}
}
_____________________________
PRODUCTOR

/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package productorconsumidor;
import java.util.Random;
/**
* Clase que representa el productor
* @author francis
*/
public class Productor implements Runnable
{
private final Random aleatorio;
private final Contenedor contenedor;

private final int idproductor;


private final int TIEMPOESPERA = 1500;
/**
* Constructor de la clase
* @param contenedor Contenedor comn a los
consumidores y el productor
* @param idproductor Identificador del productor
*/
public Productor(Contenedor contenedor, int
idproductor)
{
this.contenedor = contenedor;
this.idproductor = idproductor;
aleatorio = new Random();
}
@Override
/**
* Implementacin de la hebra
*/
public void run()
{
while(Boolean.TRUE)
{
int poner = aleatorio.nextInt(300);
contenedor.put(poner);
System.out.println("El productor " +

idproductor + " pone: " + poner);


try
{
Thread.sleep(TIEMPOESPERA);
}
catch (InterruptedException e)
{
System.err.println("Productor " +
idproductor + ": Error en run -> " +
e.getMessage());
}
}
}
}
_______________________________________
PRODUCTORCONSUMIDOR

/*
* To change this license header, choose License
Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package productorconsumidor;
/**
*
* @author francis

*/
public class ProductorConsumidor
{
private static Contenedor contenedor;
private static Thread productor;
private static Thread [] consumidores;
private static final int CANTIDADCONSUMIDORES
= 5;
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
contenedor = new Contenedor();
productor = new Thread(new
Productor(contenedor, 1));
consumidores = new
Thread[CANTIDADCONSUMIDORES];
for(int i = 0; i < CANTIDADCONSUMIDORES; i+
+)
{
consumidores[i] = new Thread(new
Consumidor(contenedor, i));
consumidores[i].start();
}

productor.start();
}
}

https://github.com/galleta/-Java_Productor_Consumidor/tree/master/src/productorconsumidor

PAGINA

You might also like