Alejandro_S_Mercado

Untitled

Sep 24th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         ArrayList <Integer> lista=new ArrayList<Integer>();
  5.             Random random=new Random();
  6.             int tiempoInicial=100;
  7.             int tiempoFinal=200;
  8.            
  9.             int consumidores=0;
  10.             int productores=0;
  11.        
  12.         for(int i=0; i<20;i++) {
  13.             int tiempoRetardo=random.nextInt((tiempoFinal-tiempoInicial)+1)+tiempoInicial;
  14.             int prodCons=random.nextInt(2); // 0 o 1
  15.            
  16.             try {
  17.                 Thread.sleep(tiempoRetardo);
  18.             } catch (InterruptedException e) {
  19.                 e.printStackTrace();
  20.             }
  21.             if(prodCons==0 && consumidores<10) {
  22.            
  23.                 Consumidor consumidorX=new Consumidor(lista,consumidores);
  24.                 consumidorX.start();
  25.                 consumidores++;
  26.             }
  27.             else if(prodCons==1 && productores<10) {
  28.  
  29.                 Productor productorX=new Productor(lista,productores);
  30.                 productorX.start();
  31.                 productores++;
  32.             }
  33.         }
  34.        
  35.        
  36.  
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment