Ir ao conteúdo
  • Cadastre-se

sulense

Membro Júnior
  • Posts

    2
  • Cadastrado em

  • Última visita

Reputação

1
  1. verdade amigo, obrigado, porém o que eu queria era a explicação das linhas do que faz cada no codigo, isso que é o problema =/
  2. 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}}}

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...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!