Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Productor extends Thread{
- private ArrayList <Integer> lista;
- private int max_size;
- private int num;
- private Random random=new Random();
- public Productor(ArrayList <Integer> lista,int num, int max_size) {
- this.lista=lista;
- this.max_size=max_size;
- this.num=num;
- }
- public void run() {
- int tiempoInicial=400;
- int tiempoFinal=800;
- while(true) {
- try {
- int elemento=random.nextInt(1000)+1;
- int tiempoRetardo=random.nextInt((tiempoFinal-tiempoInicial)+1)+tiempoInicial;
- Thread.sleep(tiempoRetardo);
- synchronized(lista) {
- while(lista.size()>=max_size) {
- System.out.println("Productor "+this.num+": Lista llena. Esperando espacio...");
- lista.wait();
- }
- 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();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment