Você está na página 1de 7

package controle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Bar {
int numClientes = 0;
int pedidosFeitos = 0;
int mesa = 1;
int numRodadas= 0;
int rodada = 0;
int consumiu = 0;
boolean podeConsumir = false;
boolean livre = true;
boolean entregaPedido = true;
boolean controla_entrada_saida = false;
boolean iniciaNovaRodada = false;
boolean pediu = false;
boolean controla_entrega = false;

int num_garcom_entrega = 0;
boolean rodadaAlterada = false;
public synchronized void altera_Rodada(){
rodada++;
System.out.println("\n\n************* Iniciou a rodada " + rodad
a + " *************\n\n");
rodadaAlterada = true;
}
public Bar(int numClientes, int numRodadas) {
this.numClientes = numClientes;
this.numRodadas = numRodadas;
}
}

class Cliente implements Runnable {


Bar b;
// Instancia do Bar
boolean pedidoFeito = false;
fez o Pedido
int identificador;

// Verifica se o cliente

public Cliente(Bar b, int i) {


this.b = b;
this.identificador = i;
}
public void fazPedido(){
//System.out.println(Thread.currentThread().getName() + " entrou
no faz pedido");

if (!pedidoFeito) {
synchronized (b) {
while (!b.livre && !pedidoFeito) {
try {
//System.out.println(Thread.curr
entThread().getName() + " aguardando para fazer o pedido");
b.wait();
} catch (InterruptedException e) {
}
}
b.livre = false;
Random gerador = new Random();
int pedido = gerador.nextInt(5000);
if(pedido < 2500){
System.out.println("FAZ PEDIDO: " + Thre
ad.currentThread().getName() + " faz pedido "+ b.pedidosFeitos+ "\t\t" + b.mesa
);
b.pediu = true;
}else{
System.out.println("NAO FAZ PEDIDO: " +
Thread.currentThread().getName() + " nao quis fazer pedido "+ b.pedidosFeitos+
"\t\t" + b.mesa);
b.pediu = false;
}
pedidoFeito = true;
b.mesa = identificador;
b.notifyAll();
}
}else{
//System.out.println(Thread.currentThread().getName() +
" j fez o pedido");
}
}
public void retiraPedido(){
synchronized (b) {
while(b.mesa != identificador){
try {
//System.out.println(Thread.currentThrea
d().getName() + " aguardando para retirar o pedido");
b.wait();
} catch (InterruptedException e) {
}
}
}
//System.out.println(Thread.currentThread().getName() + " entrou
no retira pedido");
synchronized (b) {
while (b.entregaPedido) {
try {
//System.out.println(Thread.currentThrea
d().getName() + " aguardando para retirar o pedido");
b.wait();
} catch (InterruptedException e) {
}
}

if(b.mesa == identificador){
pedidoFeito = false;
b.entregaPedido = true;
if(b.pediu == true){
System.out.println("RECEBE BEBIDA: " + T
hread.currentThread().getName() + " retirou o pedido "+ b.pedidosFeitos + "\t\tm
esa: " + b.mesa);
}else{
System.out.println("NAO RECEBE BEBIDA: "
+ Thread.currentThread().getName() + " nao fez pedido "+ b.pedidosFeitos + "\t\
tmesa: " + b.mesa);
}
b.mesa = 0;
if(b.pedidosFeitos == 0){
b.podeConsumir = true;
System.out.println("pode consumir");
}
b.notifyAll();
}
}
}
public void consomePedido(){
//System.out.println(Thread.currentThread().getName() + " entrou
no consome pedido");
if (pedidoFeito == false){
synchronized (b) {
while (b.podeConsumir == false) {
try {
//System.out.println(Thread.curr
entThread().getName() + " aguardando para consumir o pedido");
b.wait();
} catch (InterruptedException e) {
}
}
}
Random gerador = new Random();
int tempo = gerador.nextInt(5000);
System.out.println("BEBE BEBIDA: " + Thread.currentThrea
d().getName() + " levou tempo consumindo: " + tempo);
try {
b.consumiu++;
//System.out.println(b.consumiu + "consumiu");
Thread.sleep(tempo);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(b.consumiu == b.numClientes){
//System.out.println("nao pode mais consumir");
b.podeConsumir = false;
b.controla_entrada_saida = false;
b.consumiu = 0;
b.iniciaNovaRodada = true;
}
}else{
//System.out.println(Thread.currentThread().getName() +
" no recebeu a bebida");
}

}
public void run() {
while(b.numRodadas > b.rodada){
fazPedido();
retiraPedido();
consomePedido();
}
}
}
class Garcom implements Runnable
{
Bar b;
int capacidadeAtendimento;
List<Integer> listCliente; //capacidade de clientes que o garcom atende
int controleList = 0;
boolean registraNovoPedido = true;
public Garcom(Bar b, int num) {
this.b = b;
this.capacidadeAtendimento = num;
this.listCliente = new ArrayList<Integer>(capacidadeAtendimento
);
}
public void registraPedidos(){
//System.out.println(Thread.currentThread().getName() + " entrou
no registra pedido");
//if(listCliente.size() < capacidadeAtendimento){
if(registraNovoPedido){
while (listCliente.size() < capacidadeAtendimento) {
synchronized (b) {
while (b.livre) {
try {
//System.out.println(Thr
ead.currentThread().getName() + " aguardando para registrar o pedido");
b.wait();
} catch (InterruptedException e)
{
}
}
System.out.println("REGISTRA PEDIDO: " +
Thread.currentThread().getName() + " garcom registra pedido " + b.pedidosFeitos
);
listCliente.add(controleList++, b.mesa)
;
b.livre = true;
b.pedidosFeitos++;
if(listCliente.size() == capacidadeAtend
imento)
registraNovoPedido = false;
if(b.pedidosFeitos == b.numClientes){
b.controla_entrada_saida = true;
}
// Controla o comea da entrega
if(b.pedidosFeitos == b.numClientes){
b.controla_entrega = true;
b.num_garcom_entrega++;
}
b.notifyAll();

}
}
}else{
//System.out.println("No pode registrar novo pedido");
}
//}
}
public void aguarda_entrega(){
//System.out.println(Thread.currentThread().getName() + " entrou
no aguarda entrega pedido");
if(!b.controla_entrega){
synchronized (b) {
while(b.controla_entrega != true){
try {
//System.out.println(Thread.curr
entThread().getName() + " aguardando para realizar poder realizar entrega");
b.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch blo
ck
e.printStackTrace();
}
}
}
}
}
public void entregaPedidos(){
//System.out.println(Thread.currentThread().getName() + " entrou
no entrega pedido");
while (listCliente.size() > 0) {
synchronized (b) {
while (!b.entregaPedido) {
try {
//System.out.println(Thread.curr
entThread().getName() + " aguardando para entregar o pedido");
b.wait();
} catch (InterruptedException e) {
}
}
b.entregaPedido = false;
b.pedidosFeitos--;
//b.mesa = listCliente.get(--controleList);
b.mesa = listCliente.get(0);
--controleList;
System.out.println("ENTREGA BEBIDA: " + Thread.c
urrentThread().getName() + " garcom entrega pedido " + b.pedidosFeitos + "
Mesa: " + b.mesa);
listCliente.remove(0);
b.notifyAll();
if(listCliente.size() == 0)
registraNovoPedido = true;
if(b.pedidosFeitos == 0){
b.controla_entrega = false;

b.rodadaAlterada = false;
b.num_garcom_entrega--;
}
}
}
}
public void segura_garcom(){
while(b.num_garcom_entrega != 0){
synchronized (b) {
try {
System.out.println("Garcom esperando");
b.wait();
} catch (InterruptedException e) {
}
}
}
}
public void run() {
while((b.rodada < b.numRodadas)){
registraPedidos();
aguarda_entrega();
entregaPedidos();
segura_garcom();
if(b.num_garcom_entrega == 0 && !b.rodadaAlterada){
b.altera_Rodada();
b.iniciaNovaRodada = false;
}
}
}
}
public class Main {
public static void main(String[] args) {
System.out.println("************* O Bar est Aberto *************\
n\n");
System.out.println("************* Iniciou a rodada 0 ***********
**\n\n");
Bar b = new Bar(6, 3); //parametros: numero de clientes, numero
de rodadas
Thread cliente1 = new
rametros: bar, identificador cliente
Thread cliente2 = new
rametros: bar, identificador cliente
Thread cliente3 = new
rametros: bar, identificador cliente
Thread cliente4 = new
rametros: bar, identificador cliente
Thread cliente5 = new
rametros: bar, identificador cliente

Thread(new Cliente(b, 1), "Cliente1");//pa


Thread(new Cliente(b, 2), "Cliente2");//pa
Thread(new Cliente(b, 3), "Cliente3");//pa
Thread(new Cliente(b, 4), "Cliente4");//pa
Thread(new Cliente(b, 5), "Cliente5");//pa

Thread cliente6 = new Thread(new Cliente(b, 6), "Cliente6");//pa


rametros: bar, identificador cliente
//Thread cliente7 = new Thread(new Cliente(b, 7), "Cliente7");//
parametros: bar, identificador cliente
//Thread cliente8 = new Thread(new Cliente(b, 8), "Cliente8");//
parametros: bar, identificador cliente
Thread garcom1 =
metros: bar, numero de clientes
Thread garcom2 =
metros: bar, numero de clientes
Thread garcom3 =
metros: bar, numero de clientes

new
que
new
que
new
que

Thread(new Garcom(b, 2), "garcom1"); //para


o garcom pode atender
Thread(new Garcom(b, 2), "garcom2"); //para
o garcom pode atender
Thread(new Garcom(b, 2), "garcom3"); //para
o garcom pode atender

garcom1.start();
garcom2.start();
garcom3.start();
cliente1.start();
cliente2.start();
cliente3.start();
cliente4.start();
cliente5.start();
cliente6.start();
//cliente7.start();
//cliente8.start();
try {
garcom1.join();
garcom2.join();
garcom3.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
try {
cliente1.join();
cliente2.join();
cliente3.join();
//cliente4.join();
//cliente5.join();
//cliente6.join();
//cliente7.join();
//cliente8.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
System.out.println("\n\n************* O Bar Fechou *************
\n\n");
}
}

Você também pode gostar