Threads Observer

Bom dia pessoal!

Estou com uma dúvida no meu algoritmo se alguém puder me ajudar vai ser muito gratificante!!!

Bom, eu preciso receber um valor determinado de vezes o update de uma thread observada pelo algoritmo abaixo. Acontece que mesmo depois que termino de receber tudo, EndOfSimulation (EOS), o método update deste código abaixo continua de alguma forma entrando em algum loop.

Logo abaixo do código eu tenho o método aiFlag que serve para informat ao consultor da classe main que toda simulação ja foi recebida e que pode ser copiado os ArrayLists. Porém quando eu tento pela classe main dar o comando array = aiObserver.getCanais ou getCanaisAoz, simplesmente o programa emperra infinitamente…

Alguém tem alguma idéia?

Valew abracos a todos…

[code]
package aoz;

import java.util.Observer;
import java.util.Observable;
import java.util.Random;
import java.util.ArrayList;
import avisaStarter.avisaStarterThread;

/**
*

  • @author Fabio
    */
    public class aiObserver implements Observer {

    String resposta; int flag = 0; boolean go = true;

    Random random = new Random();

    ArrayList canais = new ArrayList();// cria arraylist canais

    ArrayList canaisAoz = new ArrayList();// cria arraylist canaisAoz

    leServidores lS = new leServidores();// metodo leServidores le servidores em arquivo servidores.txt
    String[] ipCanais = lS.leServidores();// recebe servidores através de um arquivo servidores.txt

    public void update(Observable o, Object arg){

     if (arg instanceof String){
         resposta = (String) arg;
         //System.out.println("\nReceived Response: "+ resposta );
    
         while(go == true){
             this.simulations(resposta);
    
             String[] teste = resposta.split(";");
    
             if(teste[0].equals("EOS")){//tentativa de evitar o loop
                 go = false;
             }
         }
     }
    

    }

    public void simulations(String simulacao){

     String[] entrada = simulacao.split(";");
    
     int canal; String arquivo; String tipoCanal; int servidor;
    
     if(entrada[0].equals("enviandoCanalSimples")){
    
         canal = Integer.parseInt(entrada[1]); arquivo = entrada[2]; tipoCanal = entrada[3];
         servidor = random.nextInt(10);
    
         canais.add(new Canais(canal, arquivo,
                 ipCanais[servidor], true, tipoCanal));
    
         String mensagem = "Simulacao Comum: Indice(" + servidor + ") | Servidor(" + ipCanais[servidor] + ")";
    
         new avisaStarterThread(mensagem).start();
     }
    
     if(entrada[0].equals("enviandoCanalAOZ")){
    
         canal = Integer.parseInt(entrada[1]); arquivo = entrada[2]; tipoCanal = entrada[3];
         servidor = random.nextInt(10);
    
         canaisAoz.add(new CanaisAOZ(canal,
                 arquivo, ipCanais[servidor], true, tipoCanal));
    
         String mensagem = "Simulacao AOZ: Indice(" + servidor + ") | Servidor(" + ipCanais[servidor] + ")";
    
         new avisaStarterThread(mensagem).start();
     }
    
     if(entrada[0].equals("EOS")){
         flag = 1;
     }
    

    }

    public int flag(){
    return flag;//0 se n√£o est√° pronto, 1 para ok!
    }

    public ArrayList getCanais(){
    return canais;
    }

    public ArrayList getCanaisAOZ(){
    return canaisAoz;
    }
    }[/code]

Amigo na hora de postar coloque seu código entre as tags “code”, clicando no botão “Code” na barra abaixo do assunto da mensagem, assim fica mais legível pra gente, abraço…

Onde o método update é chamado?

A classe abaixo chama o update!

[code]/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

package aoz;

import java.util.Observable;

/**
*

  • @author Fabio
    */
    public class aozThread extends Observable implements Runnable {

    int numTrocas, numCanais;

    public aozThread(int numCanais, int numTrocas){
    this.numCanais = numCanais;
    this.numTrocas = numTrocas;
    }

    public void run(){
    try{

     int i = 0; int tempTotal = 0; int anterior = 0;
     int aleatorio = 0; int adjacente = 0;
    
     Acao troca = new Acao(numCanais);
    
     while(i < numTrocas){
    
         String acao = troca.escolheAcao();
    
         String[] acoes = acao.split(";");
    
         String oquefazer = troca.acao(Integer.parseInt(acoes[0]));
    
         if(oquefazer.equals("aleatorio")){
             tempTotal = tempTotal + 5;
             aleatorio += 1;
    
             String temp = "enviandoCanalSimples;" + Integer.toString(Integer.parseInt(acoes[1])) + ";grande.mp3;aleatorio";
    
             setChanged();
             notifyObservers(temp);
         }
         if(oquefazer.equals("adjacente")){
             tempTotal = tempTotal + 1;
             adjacente += 1;
    
             String temp = "enviandoCanalSimples;" + Integer.toString(Integer.parseInt(acoes[1])) + ";pequeno.mp3;adjacente";
    
             setChanged();
             notifyObservers(temp);
    
         }
         if(oquefazer.equals("anterior")){
             tempTotal = tempTotal + 1;
             anterior += 1;
    
             String temp = "enviandoCanalSimples;" + Integer.toString(Integer.parseInt(acoes[1])) + ";pequeno.mp3;anterior";
    
             setChanged();
             notifyObservers(temp);
    
         }
    
         i++;
     }
    
     /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>AOZ>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
    
     acaoOtimizada aoz = new acaoOtimizada(numCanais);
    
     aoz.defineFavoritos();
     aoz.defineAgendados();
    
     int iAOZ = 0; int tempTotalAOZ = 0;
     int aleatorioAOZ = 0; int anteriorAOZ = 0;
     int adjacenteAOZ = 0; int favoritosAOZ = 0;
     int agendadosAOZ = 0; int maisAcessadosAOZ = 0;
     while(iAOZ < numTrocas){
         String acaoAOZ = aoz.escolheAcao();
    
         String[] acoesAOZ = acaoAOZ.split(";");
    
         String oqueFazer = aoz.acao(Integer.parseInt(acoesAOZ[0]));
    
         if(oqueFazer.equals("aleatorio")){
             tempTotalAOZ += 5;
             aleatorioAOZ += 1;
    
             String temp = "enviandoCanalAOZ;" + Integer.toString(Integer.parseInt(acoesAOZ[1])) + ";grande.mp3;aleatorio";
    
             setChanged();
             notifyObservers(temp);
    
         }
         if(oqueFazer.equals("adjacente")){
             tempTotalAOZ += 1;
             adjacenteAOZ += 1;
    
             String temp = "enviandoCanalAOZ;" + Integer.toString(Integer.parseInt(acoesAOZ[1])) + ";pequeno.mp3;adjacente";
    
             setChanged();
             notifyObservers(temp);
    
         }
         if(oqueFazer.equals("anterior")){
             tempTotalAOZ += 1;
             anteriorAOZ += 1;
    
             String temp = "enviandoCanalAOZ;" + Integer.toString(Integer.parseInt(acoesAOZ[1])) + ";pequeno.mp3;anterior";
    
             setChanged();
             notifyObservers(temp);
    
         }
         if(oqueFazer.equals("favoritos")){
             tempTotalAOZ += 1;
             favoritosAOZ += 1;
    
             String temp = "enviandoCanalAOZ;" + Integer.toString(Integer.parseInt(acoesAOZ[1])) + ";pequeno.mp3;favoritos";
    
             setChanged();
             notifyObservers(temp);
    
         }
         if(oqueFazer.equals("agendados")){
             tempTotalAOZ += 1;
             agendadosAOZ += 1;
    
             String temp = "enviandoCanalAOZ;" + Integer.toString(Integer.parseInt(acoesAOZ[1])) + ";pequeno.mp3;agendados";
    
             setChanged();
             notifyObservers(temp);
    
         }
         if(oqueFazer.equals("maisAcessados")){
             tempTotalAOZ += 1;
             maisAcessadosAOZ += 1;
    
             String temp = "enviandoCanalAOZ;" + Integer.toString(Integer.parseInt(acoesAOZ[1])) + ";pequeno.mp3;maisAcessados";
    
             setChanged();
             notifyObservers(temp);
    
         }
    
         iAOZ++;
     }
    
     String EOS = "EOS;EndOfSimulation;EndOfSimulation";
    
     setChanged();
     notifyObservers(EOS);
    
     }
     catch(Exception e){
         System.out.println("Excecao gerada em aozThread:\n" + e);
     }
    

    }

}

[/code]