Advertisement
Luca_G6

Caja.java

May 21st, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package tp8e3;
  2. import java.time.LocalDateTime;
  3. import java.util.Random;
  4. import java.util.concurrent.Semaphore;
  5.  
  6. public class Caja extends Thread {
  7.  
  8.     private final Semaphore DISPONIBLE;
  9.     private final String Nombre;
  10.    
  11.    
  12.     public Caja (String nombre, Semaphore disp) {
  13.         this.Nombre= nombre;
  14.         this.DISPONIBLE= disp;
  15.     }
  16.    
  17.     public  void run(){
  18.         Random r = new Random(System.currentTimeMillis());
  19.        
  20.         try {
  21.             DISPONIBLE.acquire();
  22.             synchronized(this.Nombre) {
  23.             LocalDateTime ahora= LocalDateTime.now();
  24.             System.out.println(this.Nombre +" "+"llego a la caja"+"  "+ahora.getHour()+":"+ ahora.getMinute()+":"+ ahora.getSecond());
  25.             Thread.sleep(r.nextInt(2000)+1000);
  26.             System.out.println(this.Nombre +" "+"fue atendido"+"  "+ahora.getHour()+":"+ ahora.getMinute()+":"+ ahora.getSecond());}
  27.         }catch (InterruptedException ex){
  28.             ex.printStackTrace();
  29.         }finally {
  30.             DISPONIBLE.release();
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement