Duvida em thread

2 respostas
willemarf

Thread em metodos da classe é possivel ?

Quero usar thread em metodos da classe por exemplo!

enviarMsg
recebeMsg

Para um programa em socket que estou fazendo.

Grato!

2 Respostas

hiarlay

é possivel sim, se você tiver em posse ou a seu alcance o livro deitel java, como programar, explana muito bem esse conteudo.
Utilizei o mesmo a pouco tempo para trabalho da faculdade, comunicacao client servidor, cliente, cliente. Código todo comentado.
boa sorte.

willemarf

Eu tenho

dentro do while(conexao)

eu gostaria de criar dois thread mais nao estou conseguindo …

import <a href="http://java.net">java.net</a>.<em>;

import <a href="http://java.io">java.io</a>.</em>;

import java.util.Scanner;

import socket.enviaMsg;
public class Cliente

{

Scanner teclado = new Scanner(System.in);

public int porta = 0;

public boolean conexao = true;

public String ip = 127.0.0.1;

Socket s = null;

private enviaMsg env = null;

private recebeMsg rec = null;
public Cliente() throws IOException {

    try
    {
        System.out.println("Digite a porta: ");
        porta = Integer.parseInt(teclado.nextLine());
        String mess = "";
        System.out.println("Connectando no servidor > " + ip + ":" + porta);

        try{
            s = new Socket(ip,porta);
            conexao=true;
        }catch(java.io.IOException e){
            System.out.println("Falha ao tentar connectar ao servidor!" + e.toString());
            conexao=false;
        }

        System.out.println("Connectado com sucesso!");

        InputStream i = s.getInputStream();
        BufferedReader bin = new BufferedReader(new InputStreamReader(i));
        
        OutputStream o = s.getOutputStream();
        PrintWriter pout = new PrintWriter(o, true);
                
        while(conexao){


            env = new enviaMsg(pout);
            env.start();

            rec = new recebeMsg(bin);
            rec.start();

        }


        if(mess.equals("exit") || mess.equals("quit") || mess.equals("sair") || mess.equals("fechar") )
            conexao=false;

        } catch (Exception e) {
        System.out.println(e);
        System.exit(1);
    }
    System.out.println("ConexÃo encerrada!");
    s.close();
}



public static void main(String args[]) throws IOException {
	Cliente cliente = new Cliente();
}

}

public class recebeMsg extends Thread {

public recebeMsg(BufferedReader bin) throws IOException {

    String mess = bin.readLine();
    if( mess != null ){

        try{
            System.out.println(mess);
        }catch (Exception e){


        }
        
    }


}

}

class enviaMsg extends Thread{

Scanner teclado = new Scanner(System.in);

public enviaMsg(PrintWriter pout) {




    System.out.println("> ");
    String mess = teclado.nextLine();

    if( mess != null ){

        try{
            pout.println(mess);
        } catch(Exception e) {

        }
    }
}

}

Criado 12 de maio de 2009
Ultima resposta 13 de mai. de 2009
Respostas 2
Participantes 2