【问题标题】:How stop or kill a thread in java?如何停止或杀死java中的线程?
【发布时间】:2014-12-19 21:55:14
【问题描述】:

我有问题。我正在尝试创建一个与 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]);
                    }
                }
            }

        }
    }
}

【问题讨论】:

标签: java multithreading sockets tcp


【解决方案1】:

实际上,您的线程会在您尝试中断它时尝试退出:

    try {
        answer = input.readLine();

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

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

【讨论】:

    【解决方案2】:

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

    您的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。所以你不需要明确地杀死它。

    【讨论】:

      【解决方案3】:

      线程应在适当时自行结束。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-30
        • 1970-01-01
        • 2017-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-26
        相关资源
        最近更新 更多