Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ConsumidorCliente extends Thread{
- private ArrayList <Object> mostrador;
- private int numCliente;
- private Random random;
- public ConsumidorCliente(ArrayList <Object> mostrador,int numCliente) {
- this.mostrador=mostrador;
- this.numCliente=numCliente;
- this.random=new Random();
- }
- public void run() {
- try {
- int timeConsumition=random.nextInt(400-200)+200;
- System.out.println("El cliente("+this.numCliente+") está comprando");
- boolean waitBizcocho = false;
- boolean waitFactura = false;
- Bizcocho b = null;
- Factura f = null;
- synchronized(mostrador) {
- while(true) {
- if(mostrador.isEmpty()) {
- System.out.println("El mostrador está vacío");
- System.out.println("El cliente("+this.numCliente+") debe esperar");
- mostrador.wait();
- }
- else {
- for (Object e : mostrador) {
- if (e instanceof Bizcocho && b == null) {
- b = (Bizcocho) e;
- }
- if (e instanceof Factura && f == null) {
- f = (Factura) e;
- }
- }
- if (b != null && f != null) break;
- if (b == null && !waitBizcocho) {
- System.out.println("El cliente(" + numCliente + ") debe esperar por un bizcocho");
- waitBizcocho = true;
- }
- if (f == null && !waitFactura) {
- System.out.println("El cliente(" + numCliente + ") debe esperar por una factura");
- waitFactura = true;
- }
- mostrador.wait();
- }
- }
- Thread.sleep(timeConsumition);
- mostrador.remove(b);
- mostrador.remove(f);
- System.out.println("El cliente(" + numCliente + ") ha tomado el bizcocho(" + b.getNumBizcocho() + ", " + b.getTipoBizcocho() + ")");
- System.out.println("El cliente(" + numCliente + ") ha tomado la factura(" + f.getNumFactura() + ", " + f.getTipoFactura() + ")");
- System.out.println("El cliente(" + numCliente + ") se retiró del local.");
- }
- }
- catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment