Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Principal;
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.ReentrantLock;
- /**
- *
- * @author victo
- */
- public class Tanque {
- int nPCDentro;
- int nLlantasDentro;
- CanvasT canvas;
- int nPCEsperando;
- int nLlantasEsperando;
- ReentrantLock mutex;
- Condition cola;
- // 5 LLANTAS - 2 PC - 1 PC HASTA 3 LLANTAS
- public Tanque(CanvasT c) {
- this.canvas = c;
- mutex = new ReentrantLock();
- cola = mutex.newCondition();
- }
- public void EntraLlanta(int id) throws InterruptedException {
- mutex.lock();
- try {
- while (nLlantasDentro == 5 || nPCDentro == 2 || (nPCDentro == 1 && nLlantasDentro == 3) || nPCEsperando > 0) {
- nLlantasEsperando++;
- System.out.println("ESPERA LLANTA: " + id);
- nLlantasEsperando--;
- cola.await();
- }
- nLlantasDentro++;
- canvas.entraPieza("LLANTA-", id);
- System.out.println("ENTRA LLANTA: " + id);
- System.out.println("nPCDentro=" + nPCDentro + " NLlantaDentro=" + nLlantasDentro);
- } finally {
- mutex.unlock();
- }
- }
- public void SaleLlanta(int id) {
- mutex.lock();
- try {
- nLlantasDentro--;
- System.out.println("SALE LLANTA: " + id);
- System.out.println("nPCDentro=" + nPCDentro + " NLlantaDentro=" + nLlantasDentro);
- cola.signal();
- } finally {
- mutex.unlock();
- }
- }
- public void EntraPC(int id) throws InterruptedException {
- mutex.lock();
- try{
- while (nLlantasDentro == 5 || nPCDentro == 2 || (nPCDentro == 1 && nLlantasDentro == 3)) {
- System.out.println("ESPERA PC: " + id);
- cola.await();
- }
- nPCDentro++;
- canvas.entraPieza("PC-", id);
- System.out.println("ENTRA PC: " + id);
- System.out.println("nPCDentro=" + nPCDentro + " NLlantaDentro=" + nLlantasDentro);
- }finally {
- mutex.unlock();
- }
- }
- public void SalePC(int id) {
- mutex.lock();
- try{
- nPCDentro--;
- System.out.println("SALE PC: " + id);
- System.out.println("nPCDentro=" + nPCDentro + " NLlantaDentro=" + nLlantasDentro);
- cola.signal();
- }
- finally{
- mutex.unlock();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement