title: Use threads on a RESTful Web Service in Java
body: <p>I have a problem with a RESTful Web Service, I have a client in php that call a RESTful Service in Java. In my RESTful Service I have a Post method, which executes an update query to modify all rows in a table with 10000 records . I want to use threads to do this. Is it possible to do that?. Please help me I'm a kind of new in Java. Thanks.</p>
  
  <p>OK I'm doing this in my service layer:     </p>
  
  <pre><code>for(int i = 0; i &lt; 10; i++)
  {
      startRow = i*1000;
      finalRow = i*1000 + 1000;
      Runnable process = new ProcessRecords(startRow , finalRow);
      executor.execute(process);                    
  }
  
  // Wait until all threads are finish
  while (!executor.isTerminated()) {
  
  }
  System.out.println("\n All threads finished");
  </code></pre>
  
  <p>And I'm calling this class (ProcessRecords) to execute the update:</p>
  
  <pre><code>public ProcessRecords (int start, int final)
  {
      startRow = start;
      finalRow = final;
  }
  
  @Override
  public void run(){
  
      try {
  
          ConsultUniversity consult = new ConsultUniversity ();
          consult.averangeGrade(startRow, finalRow);
  
      } catch (Exception ex) {
          Logger.getLogger(Procesos.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  </code></pre>
  
  <p>Then in my Data Layer I'm doing this inside of my method "averangeGrade":</p>
  
  <pre><code> try {
          conn = UniversityConection();//This is my conection
  
          query = "SELECT * FROM grades LIMIT " + filaInicial + "," + filaFinal;            
          prepStatement = conn.prepareStatement(query);
  
          rs = prepStatement.executeQuery(query);  
  
          rsmd = rs.getMetaData();
  
          while(rs.next())
          {
              averange = (rs.getInt("nFirGrd") + rs.getInt("nSecGrd") + 
                                 rs.getInt("nThrGrd"))/3; //the averange of three grades
  
              query = "UPDATE grades SET nFinGrd = ? WHERE cCodAlu = ?";
  
              prepStatement = conn.prepareStatement(query);
              prepStatement.setInt(1, averange);
              prepStatement.setString(2, rs.getString("cCodAlu"));
  
              prepStatement.executeUpdate();
  
              System.out.println("Record " + rs.getString("cCodAlu") + 
                                         " modified");
          }               
  
          conn.close(); //close connection    
  
      }
  </code></pre>
  
  <p>Then when I execute my client, my service does the update for the top rows, like 50 rows, then returns the message, like if all processes were finished, and I don't know why. I think is not waiting until all threads are finished, but there is code for that, then why is this happening?. Please help me. Thank you.</p>