Ir ao conteúdo
  • Cadastre-se

Problema dos fumantes [URGENTE]


sulense

Posts recomendados

Olá pessoal, por favor, alguem poderia me explicar esse código? Valeeu! :)
 
CLASS AGENTE

public class Agente extends Thread {private Mesa m; private int item1, item2;public Agente(Mesa m) { this.m = m; }public void run() {while (true) {escolherItem();while (m.calocarItensNaMesa(item1 + item2) == false);}}public void escolherItem() {item1 = (int) (Math.random() * 3);item2 = (int) (Math.random() * 3);if (item1 == item2)if (item2 == 2)item2 = 0;elseitem2++;}}

CLASS MESA

public class Mesa {private int fumante = 4;private int estado[] = { 0, 0, 0 };private final int ESPERANDO = 0;private final int FUMANDO = 1;public synchronized void fumar(int i) {while (fumante != i) {try {wait();} catch (InterruptedException e1) {e1.printStackTrace();}}if (fumante == i) {estado[i - 1] = FUMANDO;mostrarStatus();} else {System.out.println("ERROR");}}public synchronized void finalizar(int i) {estado[i - 1] = ESPERANDO;fumante = 4;notifyAll();}public synchronized boolean calocarItensNaMesa(int z) {int cont = 0;for (int x = 0; x < 3; x++)if (estado[x] != FUMANDO)cont++;if (cont == 3) {fumante = z;notifyAll();try {wait();} catch (InterruptedException e) {e.printStackTrace();} } else {return false;} return true; }public void mostrarStatus() {for (int i1 = 0; i1 < 3; i1++) {System.out.printf("F%d", (i1 + 1));switch (estado[i1]) {case ESPERANDO: {System.out.printf("ESPERANDO ");break;}case FUMANDO: {System.out.printf("FUMANDO ");break;}}}System.out.print("\n");}}

CLASS FUMANTE

public class Fumante extends Thread {private Mesa m;private final int fumante;private final int delay = 1000;public Papel(Mesa m, int fumante) {this.m = m;this.fumante = fumante;}public void run() {while (true) {m.fumar(fumante);fumar();m.finalizar(fumante);}}public void fumar(){try{Thread.sleep((int) (Math.random() *delay));}catch (InterruptedException e) {// TODO: handle exception}}}
  • Curtir 1
Link para o comentário
Compartilhar em outros sites

Boa noite, 

 

Aparentemente, esse código server para identificar quem está fumando ou não
em uma mesa de um estabelecimento comercial, como em um bar por exemplo.   

 

Obs: é ideal que idente seu código da próxima vez como a baixo:

CLASS AGENTE

public class Agente extends Thread {    private Mesa m;    private int item1, item2;    public Agente(Mesa m) {        this.m = m;    }    public void run() {        while (true) {            escolherItem();            while (m.calocarItensNaMesa(item1 + item2) == false);        }    }    public void escolherItem() {        item1 = (int) (Math.random() * 3);        item2 = (int) (Math.random() * 3);        if (item1 == item2)            if (item2 == 2)                item2 = 0;            else                item2++;    }}

CLASS MESA

public class Mesa {    private int fumante = 4;    private int estado[] = { 0, 0, 0 };    private final int ESPERANDO = 0;    private final int FUMANDO = 1;    public synchronized void fumar(int i) {        while (fumante != i) {            try {                wait();            } catch (InterruptedException e1) {                e1.printStackTrace();            }        }        if (fumante == i) {            estado[i - 1] = FUMANDO;            mostrarStatus();        } else {            System.out.println("ERROR");        }    }    public synchronized void finalizar(int i) {        estado[i - 1] = ESPERANDO;        fumante = 4;        notifyAll();    }    public synchronized boolean calocarItensNaMesa(int z) {        int cont = 0;        for (int x = 0; x < 3; x++)            if (estado[x] != FUMANDO)                cont++;        if (cont == 3) {            fumante = z;            notifyAll();            try {                wait();            } catch (InterruptedException e) {                e.printStackTrace();            }        } else {            return false;        }        return true;    }    public void mostrarStatus() {        for (int i1 = 0; i1 < 3; i1++) {            System.out.printf("F%d", (i1 + 1));            switch (estado[i1]) {            case ESPERANDO: {                System.out.printf("ESPERANDO ");                break;            }            case FUMANDO: {                System.out.printf("FUMANDO ");                break;            }            }        }        System.out.print("\n");    }}

CLASS FUMANTE

public class Fumante extends Thread {    private Mesa m;    private final int fumante;    private final int delay = 1000;    public Papel(Mesa m, int fumante) {        this.m = m;        this.fumante = fumante;    }    public void run() {        while (true) {            m.fumar(fumante);            fumar();            m.finalizar(fumante);        }    }    public void fumar(){        try{        Thread.sleep((int) (Math.random() *delay));        }catch (InterruptedException e) {            // TODO: handle exception        }    }
  • Curtir 1
Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...