title: How to count running threads in ruby server
body: <p>I want to do a 'long running' - (takes about 0.5 seconds to execute) task in a thread in a Sinatra web server. </p>
  
  <p>The web response takes about 20 ms, so if I get busy then threads will pile up...</p>
  
  <p>So I was thinking that I would do it synch if I get busy..</p>
  
  <pre><code>if (running_thread_count &gt; 10)
      stuff_that_takes_a_second()
  else
      Thread.new do
          stuff_that_takes_a_second()
      end
  end
  </code></pre>
  
  <p>How do you get the number of running threads (I want the the number of threads launched, and not done running yet) - how do you code running_thread_count?</p>
  
  <pre><code>def running_thread_count
   return Thread.list.count
  end
  </code></pre>
  
  <p>Or do I need to check for threads that are running? i.e. when a thread is finished running, will it stop coming back in the Thread.list?</p>
  
  <p>I don't want to call join as that would defeat the purpose - which is to return quickly unless we are backed up with lots of threads working.</p>