title: Java Threads: How to find the right place in code to create a new thread for a program that allows multiple clients to connect to the server
body: <p>I'm creating two program files (one client one server). The client code has to be able to create multiple clients to connect to the server (there is only one server, there can be a multiple and/or infinite number of clients). One new thread has to be created for each client that connects to the server. The way to create a client will be opening a new command prompt / mac terminal window and run the client file. Once a client is connected, it can send messages to the server. It will also receive from the server all messages sent from the other connected clients.</p>
  
  <p>We will focus on the client code for now. (no server code yet)</p>
  
  <p>In the following code there are two places where I created new threads, I am not sure which place it should be located because there are no errors when I run the program in eclipse.</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.util.*;
  import static java.nio.charset.StandardCharsets.*;
  public class ChatClient
  {
      private static Socket Socket;
      static int numberOfClients = 0;
      public static void main(String args[]) 
      {
  
  
            //If I wanted to create multiple clients, would this code go here? OR should the new thread creation be outside the while(true) loop?
            while (true)
            {
                try 
                {
                    String host = "localhost";
                    InetAddress address = InetAddress.getByName(host);
                    numberOfClients += 1;
                    Thread ChatClient1 = new Thread ()
                    {
                        public void run()
                        {   
                            ServerSocket serverSocket = null;
                            Socket socket = null;
                            try 
                            {
                                int numberofmessages = 0;
                                String[] messagessentbyotherclients = null;
                                System.out.println("Try block begins..");
                                System.out.println("Chat client is running");
                                String port_number1= args[0];
                                System.out.println("Port number is: " + port_number1);
                                int port = Integer.valueOf(port_number1);
                                System.out.println("Listening for connections..");
                                System.out.println( "Listening on port: " + port_number1 );
                                serverSocket = new ServerSocket(port);
                                for(int i = 0; i &lt; numberOfClients; i++)
                                {
                                    System.out.println(messagessentbyotherclients);
                                }
                            }
                            catch (IOException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    };
                    ChatClient1.start();
          }
          catch (IOException e)
          {
              System.out.println("I/O error: " + e);
          }
      }
  }
  </code></pre>
  
  <p>}
  My question is: Should the thread be created right underneath the main function or inside the while(true) loop? (the while true loop will eventually be exited by control D in standard input)</p>