title: Creating too many threads in Java
body: <p>I am using threads in my Java application to partially get the data (using network calls). I have a method (which is not in a threaded class), which creates a thread pool with given size <strong>(10-15 maximum)</strong> and uses them for network calls, and I am calling that method several times from a loop.</p>
  <p>When I am running this application on a slow machine (3 GB RAM, Pentium-IV), everything works fine, but when I run it on an iMac (32 GB RAM, i7 processor), it is creating too many threads, about 2,500 sometimes and throwing an out-of-memory error.</p>
  <p>I suspect that the <code>JVM</code> is not putting back the completed threads back in to the pool as soon they are finished, hence it's creating new threads.</p>
  <p>And even on the <code>iMac</code>, if I keep Thread.sleep(1000); in the <code>for</code> loop which I mentioned above everything works fine. Creating about 900 threads though.</p>
  <p>Below are the code samples from this application:</p>
  <pre><code>public ArrayList&lt;String&gt; getValuesForKeyFromMaps(String key, ArrayList&lt;Meta&gt; locations) throws InterruptedException, ExecutionException {
  
      int threadNum = locations.size(); // 10-15 at max
  
      ExecutorService executor = Executors.newFixedThreadPool(threadNum);
      List&lt;FutureTask&lt;ArrayList&lt;String&gt;&gt;&gt; taskList = new ArrayList&lt;FutureTask&lt;ArrayList&lt;String&gt;&gt;&gt;();
  
      for(final Meta location : locations){
  
          FutureTask&lt;ArrayList&lt;String&gt;&gt; futureTask_1 = new FutureTask&lt;ArrayList&lt;String&gt;&gt;(new Callable&lt;ArrayList&lt;String&gt;&gt;() {
              public ArrayList&lt;String&gt; call() throws Exception {
                  // Service call
                  return getValues(key, location);
              }
          });
          taskList.add(futureTask_1);
          executor.execute(futureTask_1);
  
      }
  
      ArrayList&lt;String&gt; values = new ArrayList&lt;String&gt;();
  
      // Wait until all results are available and combine them at the same time
      for (int j = 0; j &lt; threadNum; j++) {
          FutureTask&lt;ArrayList&lt;String&gt;&gt; futureTask = taskList.get(j);
          values.addAll(futureTask.get());
      }
      executor.shutdown();
      return values;
  }
  </code></pre>
  <p>If I call the above method using below for loop on iMac, it throws a memory error, since it's creating about 2,500 threads. But it works fine on the slower machine.</p>
  <pre><code>    for(String key : keySet){
          getValuesForKeyFromMaps(key, metaMap.get(key));
      }
  </code></pre>
  <p>And, with the below code, on <code>iMac</code> it's working fine with about 900 threads.</p>
  <pre><code>    for(String key : keySet) {
          getValuesForKeyFromMaps(key, metaMap.get(key));
          Thread.sleep(200); // Sleeping for 200 ms
      }
  </code></pre>
  <p>If I increase sleep time in the above for loop to <code>1000&amp;nbsp;ms,</code> it's creating only 30-50 threads and the application is working fine.</p>
  <p>How do I control maximum threads allowed in my application? I am intending to create/use 10-15 threads at the maximum at a given time, but Java is creating too many.</p>