Advertisement
FacundoCruz

clase Cliente

May 19th, 2022
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.Random;
  3.  
  4. public class Cliente extends Thread{
  5.    
  6.     int pos;
  7.     private List<Integer> facturas;
  8.     private List<Integer> bizcochos;
  9.    
  10.     public Cliente (List<Integer> facturas, List<Integer> bizcochos, int pos) {
  11.         this.facturas = facturas;
  12.         this.bizcochos = bizcochos;
  13.         this.pos = pos;
  14.     }
  15.    
  16.     public void run() {
  17.         Random r = new Random();
  18.         try {
  19.             System.out.println("Llega el cliente n° "+pos);
  20.            
  21.             synchronized (bizcochos) {
  22.                 while(bizcochos.isEmpty()) {
  23.                     System.out.println("Cliente n° "+pos+" esperando recibir un bizcocho.");
  24.                     bizcochos.wait();
  25.                 }
  26.                 bizcochos.remove(0);
  27.                 System.out.println("El cliente n° "+pos+" tomó un bizcocho");
  28.             }
  29.             synchronized(facturas)
  30.             {
  31.                 while(facturas.isEmpty()) {
  32.                 System.out.println("Cliente n° "+pos+" esperando recibir una factura.");
  33.                 facturas.wait();    
  34.                 }
  35.                 facturas.remove(0);
  36.                 System.out.println("El cliente n° "+pos+" tomó una factura");
  37.             }
  38.            
  39.             System.out.println("El cliente n° "+pos+" realiza la compra.");
  40.             System.out.println("Cantidad de bizcochos: "+bizcochos.size());
  41.             System.out.println("Cantidad de facturas: "+facturas.size()+"\n");
  42.             Thread.sleep(r.nextInt(400-200)+200);
  43.             System.out.println("El cliente n° "+pos+" se retira del local.");
  44.         }catch (InterruptedException e) {              
  45.             e.printStackTrace();
  46.         }
  47.     }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement