title: How can loop continue before method is finished?
body: <p>I have multiple threads(runnables) in my program. On of theme is handling RS232 communication.
  My problem is that code inside the loop is not executed in the order that is written:</p>
  
  <pre><code>while(!serialData.dataToSend.isEmpty())
  {
      try {
          SerialMsgToSend msgObject = serialData.dataToSend.remove();
          if(msgObject.type == msgObject.HOLDING_REGISTER)
          {
              Thread.sleep(COMMAND_WAIT_TIME);
              Toolkit.getDefaultToolkit().beep();
              modBusManager.singleRegisterWriteToMultipleRegisters(msgObject.unit, msgObject.startRegisterAdress, msgObject.data);
          }
          else if(msgObject.type == msgObject.COIL)
          {
              Thread.sleep(COMMAND_WAIT_TIME);
              Toolkit.getDefaultToolkit().beep();
              modBusManager.writeToCoil(msgObject.unit, msgObject.startRegisterAdress, msgObject.data[0] == 1);
          }
          Thread.sleep(5000);
          readUnitsData(msgObject.unit);
          Thread.sleep(5000);
          if(msgObject.RESPONSE &gt; 0)
          {
              serialData.listeners[msgObject.unit - 1].sendResponseToServer(msgObject.RESPONSE);
          }
      } catch (Exception ex) {
          log.error("Exception on sending data: " + ex.toString());
      }
  }
  </code></pre>
  
  <p>First I write to ModBus register with calling: </p>
  
  <pre><code>modBusManager.singleRegisterWriteToMultipleRegisters(msgObject.unit, msgObject.startRegisterAdress, msgObject.data);
  </code></pre>
  
  <p>After that I want to wait 5 seconds that registers are updated, then read them and send information to server.</p>
  
  <p>I read the data with calling method:</p>
  
  <pre><code>readUnitsData(msgObject.unit);
  </code></pre>
  
  <p>And then I am using listener to tell another thread to send data to server:</p>
  
  <pre><code>serialData.listeners[msgObject.unit - 1].sendResponseToServer(msgObject.RESPONSE);
  </code></pre>
  
  <p>My problem is that data is sent to server before it gets read/updated so I send old data. I am used that code is executed in the order that is written. Am I using threads in a wrong way or what could be the problem?</p>
  
  <p>Here is method which I call to read data:</p>
  
  <pre><code>private void readUnitsData(int unitID) 
  {
      if(mtxData.climatList[unitID] != null)
      {
          try 
          {
              log.info("Serial reading data for: " + unitID);
              int[] coils = modBusManager.readCoils(unitID + 1,0,87);
              String[] holding = modBusManager.readHoldingRegisters(unitID + 1,0,64); //(int slaveAdress, int registerAdress, int registerQuntaity)
              if(coils != null &amp;&amp; holding != null)
              {
                  System.out.println("send to listner: "  + unitID);
                  serialData.listeners[unitID].newHoldingAndCoilData(holding, coils);
              }
          } catch (Exception ex) 
          {
              log.error("Exception on run: " + ex.toString());
          }
      }
  }
  </code></pre>
  
  <p>And method inside other runnable, which is connected to listener:</p>
  
  <pre><code>@Override
  public void sendResponseToServer(int responseType) 
  {
      try
      {
          log.info("listener for sendStatusToServer called: " + responseType);
          Thread.sleep(15000);
          switch(responseType)
          {
              case 1:
                  communicationManager.sendStatus();
                  break;
              case 2:
                  communicationManager.sendSettings();
                  break;
          }
      }catch(Exception ex)
      {
          log.error("Exception on sendResponseToServer: " + ex);
      }
  }
  </code></pre>