如何停止或杀死java中的线程?

时间:2014-10-24 04:31:27

标签: java multithreading sockets tcp

我有问题。我试图创建一个与tcp服务器通信的线程。我的问题是我需要关闭线程。我怎样才能做到这一点?我试过.interrupt,.stop,那并没有用。

最好的问候

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]);
                    }
                }
            }

        }
    }
}

3 个答案:

答案 0 :(得分:1)

线程应该在适当时结束。

向该类添加一个公共标志,您可以在希望该线程终止时设置该标志。如果愿意,您可以添加获取和设置标志的方法。

然后在run()方法中添加一个循环,该方法检查标志并在适当的时候退出。

答案 1 :(得分:1)

在每个线程中都有一个中断标志。

您的while条件没问题。

所以你可以做的是,

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();  
}  

循环正常退出后,run()方法将完全执行,您的线程将自动移至dead state。所以你不需要明确杀死它。

答案 2 :(得分:0)

实际上你的线程试图在你试图打断它时退出:

    try {
        answer = input.readLine();

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

但你忽略了退出的企图。你不应该吃异常,正确处理它。