Advertisement
Guest User

Proyecto2

a guest
Jan 22nd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package Proyecto2;
  7. import java.util.concurrent.locks.Condition;
  8. import java.util.concurrent.locks.ReentrantLock;
  9.  
  10. /**
  11.  *
  12.  * @author igor_
  13.  */
  14. public class Agencia {
  15.     ReentrantLock candado;
  16.     Condition c1,c2;
  17.     private int agenteA,agenteB;
  18.     private int clientesViajes;
  19.    
  20.     public Agencia(){
  21.         agenteA=-1;
  22.         agenteB=-1;
  23.         candado = new ReentrantLock();
  24.         c1 = candado.newCondition(); // clientes de viajes
  25.         c2 = candado.newCondition(); // clientes de entradas
  26.         clientesViajes = 0;
  27.     }
  28.    
  29.     public void EntraVijae(int id){
  30.         candado.lock();
  31.         try{
  32.             while(agenteA != -1 && agenteB != -1){
  33.                 clientesViajes++;
  34.                 c1.await();
  35.                 clientesViajes--;
  36.             }
  37.             if(agenteA == -1){
  38.                 agenteA=id;
  39.             }else{
  40.                 agenteB=id;
  41.             }
  42.         }catch(Exception e){
  43.             System.out.println("Fallo den el EntraViajes: "+e.getMessage());
  44.         }
  45.         candado.unlock();
  46.     }
  47.    
  48.     public void SaleViaje(int id){
  49.         candado.lock();
  50.         if(id==agenteA){
  51.             agenteA=-1;
  52.         }else{
  53.             agenteB=-1;
  54.         }
  55.         if(clientesViajes>0){
  56.             c1.notify();
  57.         }else{
  58.             c2.notify();
  59.         }
  60.         candado.unlock();
  61.     }
  62.    
  63.     public void EntraEntradas(int id){
  64.         candado.lock();
  65.         try{
  66.             while(agenteA != -1 && agenteB != -1 || (agenteB != -1 && clientesViajes != 0)){
  67.                 c2.await();
  68.             }
  69.             if(agenteB == -1){
  70.                 agenteB=id;
  71.             }else{
  72.                 agenteA=id;
  73.             }
  74.         }catch(Exception e){
  75.             System.out.println("Fallo den el EntraViajes: "+e.getMessage());
  76.         }
  77.         candado.unlock();
  78.     }
  79.    
  80.     public void SaleEntradas(int id){
  81.         candado.lock();
  82.         if(id==agenteA){
  83.             agenteA=-1;
  84.         }else{
  85.             agenteB=-1;
  86.         }
  87.         c2.signal(); // como c2 y c1 son igual de importantes para el agenteB, los ejecutamos sin comprobaciones
  88.         c1.signal();
  89.         candado.unlock();
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement