title: How do I communicate with all threads on a Multithreaded server?
body: <p>Ok. I'm trying to grasp some multithreading Java concepts. I know how to set up a multiclient/server solution. The server will start a new thread for every connected client.</p>
  
  <p>Conceptually like this...</p>
  
  <p>The loop in <strong>Server.java:</strong></p>
  
  <pre><code>while (true) {
    Socket socket = serverSocket.accept();
    System.out.println(socket.getInetAddress().getHostAddress() + " connected");
    new ClientHandler(socket).start();
  }
  </code></pre>
  
  <p>The <strong>ClientHandler.java</strong> loop is:</p>
  
  <pre><code>while(true)
  {
      try {
           myString = (String) objectInputStream.readObject();
      } 
      catch (ClassNotFoundException | IOException e) {
          break;
      }   
  
      System.out.println(myClientAddress + " sent " + myString);
  
      try {
          objectOutputStream.writeObject(someValueFromTheServer);
          objectOutputStream.flush();
      } 
      catch (IOException e) {
          return;
      }
  }
  </code></pre>
  
  <p>This is just a concept to grasp the idea. Now, I want the server to be able to send the <em>same object</em> or data at the <em>same time</em> - to <em>all clients</em>. </p>
  
  <p>So somehow I must get the Server to speak to every single thread. Let's say I want the server to generate random numbers with a certain time interval and send them to the clients.</p>
  
  <p>Should I use properties in the Server that the threads can access? Is there a way to just call a method in the running threads from the main thread? I have no clue where to go from here.</p>
  
  <p><strong>Bonus question:</strong> 
  I have another problem too... Which might be hard to see in this code. But I want every client to be able to receive messages from the server AND send messages to the sever independently. Right now I can get the Client to stand and wait for my gui to give something to send. After sending, the Client will wait for the server to send something back that it will give to the gui. You can see that my ClientHandler has that problem too.</p>
  
  <p>This means that while the Client is waiting for the server to send something it cannot send anything new to the server. Also, while the Client is waiting for the gui to give it something to send, it cannot receive from the server.  </p>
  
  <p>I have only made a server/client app that uses the server to process data it receives from the Client - and the it sends the processed data back.</p>
  
  <p>Could anyone point me in any direction with this? I think I need help how to think conceptually there. Should I have two different ClientHandlers? One for the instream and one for the outstream? I fumbling in the dark here.</p>