Results 1 to 1 of 1
Thread: Problem with Treaths
- 11-20-2011, 09:01 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Problem with Treaths
i have 2 threads, one to update a iplist in 60sec. to 60sec. and another to send a message in 30sec to 30sec but when i execute the main class the program execute a infinite cicle and the threads don´t stopped how wish them to.
Tread4Java Code:public class Main { ServidorPresencas srv; static boolean closed = false; public static void main(String[] args) { // try{ Jogador joga = new Jogador(); String estado = "free"; String novoNome2 = JOptionPane.showInputDialog("Introduza o seu nome"); joga.setMyName(novoNome2); joga.setMyState(estado); UDPReceive re = new UDPReceive(joga); RecebeTCP socket = new RecebeTCP(); Simulador sim = new Simulador(joga); Thread t1 = new Thread(re); Thread t2 = new Thread(socket); Thread t3 = new Thread(sim); t1.start(); t2.start(); t3.start(); while (true) { PLAYER_INFO info = new PLAYER_INFO(joga); ServidorPresencas srv = new ServidorPresencas(joga); Thread t4 = new Thread(info); Thread t5 = new Thread(srv); t5.start(); t4.start(); }} catch (Exception e) { System.err.println("IOException: " + e); } } }
Thread5Java Code:public class ServidorPresencas implements Runnable { public String[] utilizadoresOnline; Jogador joga; UDPRecebe recebe = new UDPRecebe(joga); public HashMap<String, String[]> info = recebe.info; public ServidorPresencas(Jogador joga) { this.joga = joga; } public void run() { ligarServidor(); Boolean continuar = true; while (continuar) { getInfo(); } } private void ligarServidor() { try { URL servPresencas = new URL("http://193.137.8.61/heartbeat/heartbeat.svc/show?ip=" + InetAddress.getLocalHost().getHostAddress()); URLConnection yc = servPresencas.openConnection(); try (BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream()))) { String inputLine; while ((inputLine = in.readLine()) != null) { utilizadoresOnline = inputLine.split(";"); } listarUtilizadores(); } try { synchronized (this) { Thread.sleep(60000); notifyAll(); } } catch (InterruptedException e) } catch (IOException ex) { System.out.println(ex); } } private void listarUtilizadores() { for (int i = 0; i < utilizadoresOnline.length; i++) { System.out.println(info); System.out.println("o ip: " + utilizadoresOnline[i] + ""); } }
Java Code:public void Player_info() { for (String str : utilizadoresOnline) { try { UDPEnvia.enviaMsg(str, "PLAYER_INFO " + joga.getName() + "; " + joga.getMyState()); } catch (Exception ex) { ex.printStackTrace(); } } try { synchronized (this) { Thread.sleep(60000); notifyAll(); } } catch (InterruptedException e) { System.out.println("ERROR: " + ServidorPresencas.class.getName() + " : " + e); } // Logger.getLogger(ServidorPresencas.class.getName()).log(Level.SEVERE, null, ex); } }


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks