title: Limit number of threads in Groovy
body: <p>How can I limit number of threads that are being executed at the same time?
  Here is sample of my algorithm:</p>
  
  <pre><code>for(i = 0; i &lt; 100000; i++) {
      Thread.start {
          // Do some work
      }
  }
  </code></pre>
  
  <p>I would like to make sure that once number of threads in my application hits 100, algorithm will pause/wait until number of threads in the app goes below 100. </p>
  
  <p>Currently "some work" takes some time to do and I end up with few thousands of threads in my app. Eventually it runs out of threads and "some work" crashes. I would like to fix it by limiting number of pools that it can use at one time. </p>
  
  <p>Please let me know how to solve my issue.</p>