Alejandro_S_Mercado

Untitled

Sep 24th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. public class ProductorFactura extends Thread{
  2.     private ArrayList <Object> mostrador;
  3.     private int num;
  4.     private String[] types;
  5.     private String type;
  6.     private Random random;
  7.    
  8.     public ProductorFactura(ArrayList <Object> mostrador) {
  9.         this.mostrador=mostrador;
  10.         this.num=0;
  11.         this.types=new String[]{"con chocolate","simple","con coco"};
  12.         this.type="";
  13.         this.random=new Random();
  14.     }
  15.    
  16.     public void run() {
  17.    
  18.         while(true) {
  19.         try {
  20.             int timeProduction=random.nextInt(1300-1000)+1000;
  21.             int typeRandom=random.nextInt(this.types.length);
  22.            
  23.             Thread.sleep(timeProduction);
  24.             synchronized(mostrador) {
  25.                 this.num++;
  26.                 this.type=types[typeRandom];
  27.                 Factura factura=new Factura(this.num,this.type);
  28.                 mostrador.add(factura);
  29.                 mostrador.notifyAll();
  30.             }
  31.            
  32.         }catch (InterruptedException e) {
  33.             e.printStackTrace();
  34.         }
  35.        
  36.         System.out.println("Factura("+num+", "+this.type+")"+" en mostrador");
  37.         }
  38.        
  39.     }
  40.    
  41.  
  42. }
  43.  
  44.  
  45. public class ProductorBizcocho extends Thread{
  46.  
  47.     private ArrayList <Object> mostrador;
  48.     private int num;
  49.     private String[] types;
  50.     private String type;
  51.     private Random random;
  52.    
  53.     public ProductorBizcocho(ArrayList <Object> mostrador) {
  54.         this.mostrador=mostrador;
  55.         this.num=0;
  56.         this.types=new String[]{"Cuadrado","chato","relleno"};
  57.         this.type="";
  58.         this.random=new Random();
  59.     }
  60.    
  61.     public void run() {
  62.    
  63.         while(true) {
  64.         try {
  65.             int timeProduction=random.nextInt(600-400)+400;
  66.             int typeRandom=random.nextInt(this.types.length);
  67.            
  68.             Thread.sleep(timeProduction);
  69.             synchronized(mostrador) {
  70.                 this.num++;
  71.                 this.type=types[typeRandom];
  72.                 Bizcocho bizcocho=new Bizcocho(this.num,this.type);
  73.                 mostrador.add(bizcocho);
  74.                 mostrador.notifyAll();
  75.             }
  76.            
  77.         }catch (InterruptedException e) {
  78.             e.printStackTrace();
  79.         }
  80.        
  81.         System.out.println("Bizcocho("+this.num+", "+this.type+") en mostrador");
  82.         }
  83.        
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment