Advertisement
LEANDRONIEVA

Clase Mostardor

Oct 14th, 2023
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 KB | None | 0 0
  1. import java.util.LinkedList;
  2.  
  3. public class Mostrador<T> {
  4.  
  5.     private LinkedList<Integer> bizcochos = new LinkedList<>();
  6.     private LinkedList<Integer> facturas = new LinkedList<>();
  7.  
  8.     public Mostrador() {
  9.        
  10.     }
  11.    
  12.     public synchronized boolean noHayBizc() {
  13.         if (bizcochos.size()<=0) return true;
  14.         notifyAll();
  15.         return false;
  16.     }
  17.    
  18.     public synchronized boolean noHayFact() {
  19.         if (facturas.size()<=0) return true;
  20.         notifyAll();
  21.         return false;
  22.     }
  23.    
  24.     public synchronized void agregarBizcocho(int elemento) {
  25.          bizcochos.addLast(elemento);
  26.          notifyAll();
  27.     }
  28.    
  29.     public synchronized void agregarFactura(int elemento) {
  30.         facturas.addLast(elemento);
  31.         notifyAll();
  32.     }
  33.    
  34.     public synchronized int comprarBizcocho() {
  35.         while(bizcochos.size()<=0) {
  36.             try {
  37.                 wait();
  38.             } catch (InterruptedException e) {
  39.                 // TODO Auto-generated catch block
  40.                 e.printStackTrace();
  41.             }
  42.         }
  43.           int elemento = bizcochos.removeFirst();
  44.           notifyAll();
  45.           return elemento;
  46.     }
  47.    
  48.     public synchronized int comprarFactura() {
  49.         while(facturas.size()<=0) {
  50.             try {
  51.                 wait();
  52.             } catch (InterruptedException e) {
  53.                 // TODO Auto-generated catch block
  54.                 e.printStackTrace();
  55.             }
  56.         }
  57.           int elemento = facturas.removeFirst();
  58.           notifyAll();
  59.           return elemento;
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement