title: Starting thread in a servlet, what can be the issues?
body: <p>I have a web application, that, on a single request may require to load hundreds of data. Now the problem is that data is scattered. So, I have to load data from several places, apply filters on them, process them and then respond. Performing all these operations sequentially makes servlet <strong><em>slow</em></strong>!</p>
  
  <p>So I have thought of loading all the data in separate threads like <code>t[i] = new Thread(loadData).start();</code>, waiting for all threads to finish using <code>while(i &lt; count) t[i].join();</code> and when done, join the data and respond.</p>
  
  <p>Now I am not sure if this approach is right or there is some better method. I have read somewhere is that spawning thread in servlets is not advisable.</p>
  
  <p>My desired code will look something like this.</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
  {
         Iterable&lt;?&gt; requireddata = requiredData(request);
         Thread[] t = new Thread[requireddata.size];
         int i = 0;
         while (requireddata.hasNext())
         {
               t[i]  = new Thread(new loadData(requiredata.next())).start();
               i++;
         }
         for(i = 0 ; i &lt; t.length ; i++)
           t[i].join();
         // after getting the data process and respond!
  }
  </code></pre>