Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 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 Principal;
  7.  
  8. import java.util.concurrent.locks.Condition;
  9. import java.util.concurrent.locks.ReentrantLock;
  10.  
  11. /**
  12.  *
  13.  * @author victo
  14.  */
  15. public class Tanque {
  16.  
  17.     int nPCDentro;
  18.     int nLlantasDentro;
  19.     CanvasT canvas;
  20.     int nPCEsperando;
  21.     int nLlantasEsperando;
  22.     ReentrantLock mutex;
  23.     Condition cola;
  24.  
  25.     // 5 LLANTAS - 2 PC - 1 PC HASTA 3 LLANTAS
  26.     public Tanque(CanvasT c) {
  27.         this.canvas = c;
  28.         mutex = new ReentrantLock();
  29.         cola = mutex.newCondition();
  30.  
  31.     }
  32.  
  33.     public void EntraLlanta(int id) throws InterruptedException {
  34.         mutex.lock();
  35.         try {
  36.             while (nLlantasDentro == 5 || nPCDentro == 2 || (nPCDentro == 1 && nLlantasDentro == 3) || nPCEsperando > 0) {
  37.                 nLlantasEsperando++;
  38.                 System.out.println("ESPERA LLANTA: " + id);
  39.                 nLlantasEsperando--;
  40.                 cola.await();
  41.  
  42.             }
  43.             nLlantasDentro++;
  44.             canvas.entraPieza("LLANTA-", id);
  45.             System.out.println("ENTRA LLANTA: " + id);
  46.             System.out.println("nPCDentro=" + nPCDentro + " NLlantaDentro=" + nLlantasDentro);
  47.         } finally {
  48.             mutex.unlock();
  49.         }
  50.     }
  51.  
  52.     public void SaleLlanta(int id) {
  53.         mutex.lock();
  54.         try {
  55.             nLlantasDentro--;
  56.             System.out.println("SALE LLANTA: " + id);
  57.             System.out.println("nPCDentro=" + nPCDentro + " NLlantaDentro=" + nLlantasDentro);
  58.             cola.signal();
  59.         } finally {
  60.             mutex.unlock();
  61.         }
  62.     }
  63.  
  64.     public void EntraPC(int id) throws InterruptedException {
  65.         mutex.lock();
  66.         try{
  67.             while (nLlantasDentro == 5 || nPCDentro == 2 || (nPCDentro == 1 && nLlantasDentro == 3)) {
  68.                 System.out.println("ESPERA PC: " + id);
  69.                 cola.await();
  70.             }
  71.             nPCDentro++;
  72.             canvas.entraPieza("PC-", id);
  73.             System.out.println("ENTRA PC: " + id);
  74.             System.out.println("nPCDentro=" + nPCDentro + " NLlantaDentro=" + nLlantasDentro);
  75.         }finally {
  76.             mutex.unlock();
  77.         }
  78.  
  79.     }
  80.  
  81.     public void SalePC(int id) {
  82.         mutex.lock();
  83.         try{
  84.         nPCDentro--;
  85.             System.out.println("SALE PC: " + id);
  86.             System.out.println("nPCDentro=" + nPCDentro + " NLlantaDentro=" + nLlantasDentro);
  87.             cola.signal();
  88.         }
  89.         finally{
  90.             mutex.unlock();
  91.         }
  92.     }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement