0

I've a problem. I'm trying to create a thread for communcation with a tcp server. My problem is that i need to shutdown the thread. How can I do this? I tried .interrupt, .stop, and that didn't work.

Best Regards

Chat thread = new Chat(socket, reuniao[0], username);

public class Chat implements Runnable{

    Socket socket;
    boolean corre;
    String reuniao, username;
    BufferedReader input;
    PrintWriter out;
    Scanner sc;

    public Chat(Socket socket, String reuniao, String username) {
        this.socket = socket;
        this.reuniao = reuniao;
        this.username = username;

        corre = true;

        new Thread(this, "").start();
    }

    @Override
    public void run() {

        sc = new Scanner(System.in);

        try {
            input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            out = new PrintWriter(socket.getOutputStream(), true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String answer = null;

        out.println("Download Chat#"+reuniao);

        try {
            answer = input.readLine();

        } catch (IOException e) {
                    // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("I'm ready!");

        ArrayList< String > salaChat = new ArrayList< String >();

        String [] chatCompleto = answer.split("#");

        for(int i = 0; i < chatCompleto.length; i++){
            salaChat.add(chatCompleto[i]);
            System.out.println(chatCompleto[i]);
        }

        while(!Thread.currentThread().isInterrupted()){

            if(corre == false) return;

            out.println("Download Chat#"+reuniao);

            try {
                answer = input.readLine();

            } catch (IOException e) {
                        // TODO Auto-generated catch block
                e.printStackTrace();
            }

            String [] temp = answer.split("#");

            for(int i = 0; i < temp.length; i++){
                int testa = 0;
                for(int j = 0; j < salaChat.size(); j++){
                    if(temp[i].equals(salaChat.get(j))){
                        testa = 1;
                    }
                }
                if(testa == 0){
                    salaChat.add(temp[i]);
                    String [] aux = temp[i].split(":");
                    if(!username.equals(aux[0]) && temp[i] != null){
                        System.out.println(temp[i]);
                    }
                }
            }

        }
    }
}
Danielson
  • 2,605
  • 2
  • 28
  • 51
MiguelVeloso
  • 125
  • 1
  • 8

3 Answers3

1

Threads should end themselves when appropriate.

Add a public flag to the class, which you can set when you want the thread to terminate. You can add methods to get and set the flag if you wish.

Then add a loop to the run() method which checks the flag and exits when appropriate.

CharlieS
  • 1,432
  • 1
  • 9
  • 10
1

In every thread, there is an interrupt flag.

Your while condition is fine.

So what you can do is,

while(!Thread.currentThread().isInterrupted()) {  

/* Interrupt the current thread if socket connection is broken. 
   This will interrupt the thread and the condition in the while loop will become false.  
   Hence, your loop will exit normally. */  

    if(socket.isClosed())
        Thread.currentThread().interrupt();  
}  

After the loop exited normally, the run() method will be executed completely and your thread will automatically move to dead state. So you will not need to kill it explicitly.

Aditya Singh
  • 2,343
  • 1
  • 23
  • 42
0

Actually your thread tries to exit on your attempt to interrupt it:

    try {
        answer = input.readLine();

    } catch (IOException e) {
                // TODO Auto-generated catch block
        e.printStackTrace(); // <<< HERE <<<
    }

But you ignore its attempt to exit. You SHOULD NOT eat an exception, handle it correctly.

ursa
  • 4,404
  • 1
  • 24
  • 38