0

我制作了一个简单的聊天应用程序,它使用线程连接服务器和客户端。我想向所有活跃的客户发送消息。如何向活动线程列表发送消息?我使用方法flush()但未能向所有活动客户端发送消息我找到了一种在google中显示线程列表的方法,如下所示:

public static void listThreads(ThreadGroup group, String indent) {
    System.out.println(indent + "Group[" + group.getName() + 
                    ":" + group.getClass()+"]");
    int nt = group.activeCount();
    Thread[] threads = new Thread[nt*2 + 10]; //nt is not accurate
    nt = group.enumerate(threads, false);

    // List every thread in the group
    for (int i=0; i<nt; i++) {
        Thread t = threads[i];
        System.out.println(indent + "  Thread[" + t.getName() 
                    + ":" + t.getClass() + "]");
    }

    // Recursively list all subgroups
    int ng = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[ng*2 + 10];
    ng = group.enumerate(groups, false);

    for (int i=0; i<ng; i++) {
        listThreads(groups[i], indent + "  ");

}
}
}

发送消息的方法:

class ChatThread extends Thread{
    static Vector<ChatThread> chatthread = new Vector<ChatThread>(2);
    private String rslt;
    private BufferedReader in;
    private PrintWriter out;
    private Socket sock;


    public ChatThread (Socket socket) throws IOException {
        this.sock = socket;
        in  = new BufferedReader(
              new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(
              new OutputStreamWriter(socket.getOutputStream())); }

    public void run(){


        String line;
        synchronized(chatthread) {
        chatthread.addElement(this); }
        String portnum = Integer.toString(sock.getPort());

        try {

        line = in.readLine()+portnum;
        String[] mssgin = line.split("\\.");

        for(int i = 0; i < chatthread.size(); i++) {

                ChatThread handler =
                (ChatThread)chatthread.elementAt(i);
                handler.out.println(line + "\r");

                if(teksmasuk[0].contentEquals("login")){
                    MysqlConn ceklogin = new MysqlConn();
                    rslt = ceklogin.login(line); 
                    System.out.println(rslt);
                    handler.out.flush();


                }else if(mssgin[0].contentEquals("reg")){
                    Registrasi regis = new Registrasi();
                    rslt = regis.register(line);
                    System.out.println(rslt);
        handler.out.flush();
                }
                else {          
                System.out.println("Waiting...");
                }               

        }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
        } 
        finally {
            try {
                in.close();
                out.close();
                sock.close();
                } catch(IOException ioe) {
                } finally {
                synchronized(chatthread) {
                chatthread.removeElement(this);
                }
                }
        }

    }

}
4

1 回答 1

0

这有一些一般的设计问题。您不应该尝试向特定线程发送消息;您应该尝试向特定客户端发送消息。尝试将线程彼此完全隔离;他们唯一的互动应该是通过他们彼此建立的流。

于 2012-05-11T04:44:29.157 回答