Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to send a message to the list of active threads?
    text
    copied!<p>I made a simple chat application that connects the server and client using the thread. I want to send a message to all active clients. how to send a message to the list of active threads? I use the method flush () but failed to send message to all active clients I found a method to display the thread list in google as follows:</p> <pre><code>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&lt;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&lt;ng; i++) { listThreads(groups[i], indent + " "); } } } </code></pre> <p>method to send message:</p> <pre><code>class ChatThread extends Thread{ static Vector&lt;ChatThread&gt; chatthread = new Vector&lt;ChatThread&gt;(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 &lt; 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); } } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload