public class Productor extends Thread{
private ArrayList lista;
private int num;
private Random random=new Random();
public Productor(ArrayList lista,int num) {
this.lista=lista;
this.num=num;
}
public void run() {
int tiempoInicial=1000;
int tiempoFinal=1500;
while(true) {
try {
int elemento=random.nextInt(1000)+1;
int tiempoRetardo=random.nextInt((tiempoFinal-tiempoInicial)+1)+tiempoInicial;
Thread.sleep(tiempoRetardo);
synchronized(lista) {
lista.add(elemento);
//lista.notify(); // Notificar a un consumidor que hay un nuevo elemento disponible
System.out.println("Productor "+this.num+" produjo: "+elemento);
lista.notifyAll(); // Notificar a todos los consumidores que hay un nuevo elemento disponible
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}