title: Rate limiting the number of threads in an Executor (Java)
body: <p>How can the number of threads executing in an <code>Executor</code> be limited? For example, in the code bellow:</p>
  
  <pre><code>            executor = Executors.newFixedThreadPool(this.noOfThreads);
              for (int i = 0; i &lt; sections.size(); i++) {
                  DomNode section = sections.get(i);
                  // Retrieve the url of the subsection
                  String subsectionUrl = section.getNodeValue();
  
                  if(i != 0 &amp;&amp; (i % noOfThreadsPerSection == 0)) {
                      // Add section threads to the executor
                      executor.execute(new BrowseSection(filter, webClient, subsectionUrl));
                  } else {
                      executor.shutdown();
                      try {
                          executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
                      } catch (InterruptedException e) {
                          // We should normally never get here
                      }
                  }
              }
  </code></pre>
  
  <p>when <code>executor.execute()</code> is called, what happens if the thread pool is filled with running threads? </p>
  
  <p>The code above does not check whether threads are still running in the executor, but rather the number of threads that have been launched. </p>