Вы находитесь на странице: 1из 10

Ejemplo del Productor- Consumidor este programa tiene la clase Lee.

java que
es la que lee un archivo.

import java.io.*;

abstract class Lee{

public static char car(){

int charInt=-1;

try{

charInt=System.in.read();

catch (IOException e){

System.out.println(e.getMessage());

System.out.println("fatal, error. Ending


program");

System.exit(0);

return (char)charInt;

public static int entero(){

char nextChar;

String result="";

boolean valor=false;

while(!valor){

nextChar=car();

if(nextChar=='\n')

valor=true;

else if(nextChar=='\r'){}

else

result+=nextChar;
}

try{

return Integer.parseInt(result);

catch(NumberFormatException e){

System.out.println("error, formato de numero no


valido!");

return 0;

public static String cadena(){

char nextChar;

String result="";

boolean done=false;

while(!done){

nextChar=car();

if (nextChar=='\n')

done=true;

else if(nextChar=='\r'){}

else

result+=nextChar;

return result;

Clase Productor Consumidor


class Bodega

private int n=20;

private int cont=0;

private int buffer[] = new int[n];

public synchronized void Poner(int v)

while(cont==n)

try

wait();

}catch(InterruptedException e)

System.out.println("Error: "+e.toString());

buffer[cont]=v;

System.out.println("\n\t\t\tSe ingreso el elemento:


"+v);

cont++;

if(cont==1)

notify();

}
public synchronized void Sacar()

int valor;

while(cont==0)

try

wait();

}catch(InterruptedException e)

System.out.println("Error: "+e.toString());

cont--;

valor=buffer[cont];

System.out.println("\n\t\t\tEl elemento a retirar es:


"+valor);

if(cont==n-1)

notify();

}
class Productor extends MyObject2 implements Runnable{

private Bodega almacen;

public Productor(Bodega almacen){

this.almacen=almacen;

public void run(){

int num;

while(true)

num=((int)(Math.random()*200));

almacen.Poner(num);

nap(200);

class Consumidor extends MyObject2 implements Runnable {


private Bodega almacen;

public Consumidor(Bodega almacen){

this.almacen=almacen;

public void run(){

while(true)

almacen.Sacar();

nap(400);

class Principal extends MyObject2 {

public static void main (String args[]){

int runTime=60,n;
System.out.println("\n\t\t\t*******Productor-
Consumidor*******");

System.out.println("\t\t\t-------- (Buffer limitado)


------\n\n");

Bodega almacen=new Bodega();

Thread Hilo_Produce= new Thread(new Productor(almacen));

Thread Hilo_Consume= new Thread(new


Consumidor(almacen));

Hilo_Produce.start();

Hilo_Consume.start();

nap(runTime*300); //Tiempo de ejecución

Hilo_Produce.stop();

Hilo_Consume.stop();

System.out.println("\n\nEl programa termino


correctamente....!");

System.exit(0);

}
Clase MyObject

import java.io.Serializable;

import java.util.Date;

import java.util.Random;

public abstract class MyObject2 extends Object implements


Serializable{

private static final long startTime =


System.currentTimeMillis();

private static final Random rnd = new Random();

private String name = "MyObject2";

protected MyObject2() {super();}

protected MyObject2(String name){

super();

this.name = name;

protected final String getName() {return name;}

protected static final String getThreadName(){

return Thread.currentThread().getName();

protected static final long age(){


return System.currentTimeMillis() - startTime;

protected static final int nap(int napTimeMs){

long napStart = age();

try{

Thread.sleep(napTimeMs);

}catch (InterruptedException e){

// System.err.println("interrupted out of sleep");

return (int) (age()- napStart - (long) napTimeMs);

//return double in range [0,1]

protected static final double random(){

return rnd.nextDouble();

protected static final double random(int ub){

return rnd.nextDouble()*ub;

protected static final double random(int lb, int ub){


return lb + rnd.nextDouble()*(ub-lb);

//syntactic sugar por semaphores

Ejecucion del programa

Вам также может понравиться