如何停止线程的所有实例

时间:2014-06-06 18:48:59

标签: java multithreading sockets

我正在用Java开发一个基于套接字的游戏,以竞争的方式讨论谜语。

服务器程序为每个玩家(客户端)创建一个除了其他线程之外的响应线程,我想做的是在玩家发送正确的响应后停止(或中断)所有这些响应线程。

这是我的代码

public class testReponse implements Runnable {
    private Socket socket;
    BufferedReader in;
    PrintWriter out;
    String reponse="";

    public testReponse(Socket socket2){
            socket = socket2;
    }

    @Override
    public void run() {
        while(!reponse.equals("right")){
            try {
                in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
                out = new PrintWriter(socket.getOutputStream());

                String reponse = in.readLine();
                System.out.println("Reponse : "+ reponse);

                if(reponse.equals("right")){    
                    out.println("correct");
                    out.flush();
                } else {
                    out.println("incorrect");
                    out.flush();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

目前尚不清楚您的服务器代码在哪里。但是,我这样做的方法是在服务器代码中使用AtomicBoolean作为实例变量。一旦"对"从任何客户端收到消息,该值将更改为false。在服务器端的代码中,如果您看到值为false,则停止!

这是一种方法,但可能有更好的方法来做到这一点。

public class MyServer {
    private AtomicBoolean keepServerOn = new AtomicBoolean(true);

    public void setKeepServerOff() {
        keepServerOn.set(false);
    }

    public void shouldKeepGoing() {
        return keepServerOn.get();
    }

    public static void main(Strings[] args) {
    ....// where you accept clients and create TestResponse
    MyServer myServer = new MyServer();
    ...// somewhere new TestResponse(socket, myServer);
    }
}


public class testReponse implements Runnable {
private MyServer server;
private Socket socket;
private AtomicBoolean keepServerOn = new AtomicBoolean(true);

public testReponse(Socket socket2, MyServer server){
        socket = socket2;
}

@Override
public void run() {
    BufferedReader in = null;
    PrintWriter out = null;

    try {
        in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
        out = new PrintWriter(socket.getOutputStream());

        while(server.shouldKeepGoing()){
            String reponse = in.readLine();
            System.out.println("Reponse : "+ reponse);

            if(reponse.equals("right")){    
                server.setKeepServerOff();
                out.println("correct");
                out.flush();
            } else {
                out.println("incorrect");
                out.flush();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(out!= null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}