title: Multithreaded data transfer via an array in Java
body: <p>So, I guess that this has been answered before, but I couldn't find the question, so forgive me.</p>
  
  <p>I have a rather basic chat client-server pair, of which the server is multithreaded to allow for several clients to connect at the same time. The server code looks like this...</p>
  
  <pre><code>private void loop(int port) {
          // Opens a port for connections.
      ServerSocket serverSocket = null;
      Socket clientSocket = new Socket();
      try {
          serverSocket = new ServerSocket(port);
          System.out.println("Server running in port " + port);
      } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
  
          // Listens for a connection
      while (onlineState == true &amp;&amp; serverSocket != null) {
          if (cur_players &lt; max_players) {
              try {
  
                  clientSocket = serverSocket.accept();
                  System.out.println(clientSocket.getInetAddress() + " has connected to the port " + clientSocket.getPort());
                  cur_players++;
  
                  new Thread(new SocketThread( clientSocket, Chat.getOpenSeat() )).start();
  
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
      }
  }
  </code></pre>
  
  <p>Here, what <em>Chat.getOpenSeat()</em> does is browse an array(boolean seats[]) through for an open spot in the chat buffer array (String buffer[][]) and returns an integer for the spot, then marking it taken. However, when I access the buffer array from the threads, the thread only finds the messages it has added itself. Below is the corresponding code.</p>
  
  <pre><code>toClient = c.poll(bufferSocket);               // Retrieves the top-most message from the seat's sub-array,
                                                 // then bumps the remaining messages up in the sub-array.
  
  if (toClient != null) {
      out.println(toClient);                     // Sends the message through the Socket.
      System.out.println("Message was sent.");
      toClient = null;
  }
  </code></pre>
  
  <p>Curiously enough, the threads can access the <em>seats[]</em> array without any trouble, finding the currently active seats and correctly giving all the corresponding sub-arrays their messages. Here's also the bit of code I use to add a new message to the array:</p>
  
  <pre><code>public void offer(String msg) {
  
      for (int seat = 0; seat &lt; Server.max_players; seat++) {
          if (seats[seat] == true) {
              for (int i = 0; i &lt; 100; i++) {
                  if (msgBuffer[seat][i] == null) {
                      msgBuffer[seat][i] = msg;
                      System.out.println("Message: '" + msg + "' was buffered for the Seat " + seat + ".");
                      break;
                  }
              }
          }
      }
  
  }
  </code></pre>
  
  <p>So, how do I add Strings to an array that is commonly read-write accessible to all of my threads?</p>
  
  <p>Just ask if you need to see more of the code.</p>