Alejandro_S_Mercado

Untitled

Sep 24th, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. public class Persona extends Thread {
  2.     private int id;
  3.     private Pasillo pasillo;
  4.     private Random random;
  5.    
  6.     public Persona(int id,Pasillo pasillo) {
  7.         this.id=id;
  8.         this.pasillo=pasillo;
  9.         this.random=new Random();
  10.     }
  11.    
  12.     public void run() {
  13.        
  14.         System.out.println("Persona " + id + " hace fila para entrar.");
  15.        
  16.         try {
  17.             int timePermanency=random.nextInt(700-400)+400;
  18.            
  19.             synchronized(pasillo) {
  20.                 pasillo.entry(id);
  21.                 System.out.println("Persona " + id + " está dentro del Zoo por " + timePermanency + "ms.");
  22.                 Thread.sleep(timePermanency);
  23.                 System.out.println("Persona " + id + " hace fila para salir.");
  24.                 pasillo.exit(id);  
  25.             }
  26.            
  27.             System.out.println("Persona " + id + " ha salido del Zoo.");
  28.            
  29.         }catch(InterruptedException e) {
  30.             e.printStackTrace();
  31.         }
  32.     }
  33.    
  34. }
Advertisement
Add Comment
Please, Sign In to add comment