title: Java thread creation overhead
body: <p>Conventional wisdom tells us that high-volume enterprise java applications should use thread pooling in preference to spawning new worker threads. The use of <code>java.util.concurrent</code> makes this straightforward.</p>
  
  <p>There do exist situations, however, where thread pooling is not a good fit. The specific example which I am currently wrestling with is the use of <code>InheritableThreadLocal</code>, which allows <code>ThreadLocal</code> variables to be "passed down" to any spawned threads. This mechanism breaks when using thread pools, since the worker threads are generally not spawned from the request thread, but are pre-existing.</p>
  
  <p>Now there are ways around this (the thread locals can be explicitly passed in), but this isn't always appropriate or practical. The simplest solution is to spawn new worker threads on demand, and let <code>InheritableThreadLocal</code> do its job.</p>
  
  <p>This brings us back to the question - if I have a high volume site, where user request threads are spawning off half a dozen worker threads each (i.e. not using a thread pool), is this going to give the JVM a problem? We're potentially talking about a couple of hundred new threads being created every second, each one lasting less than a second. Do modern JVMs optimize this well?  I remember the days when object pooling was desirable in Java, because object creation was expensive. This has since become unnecessary. I'm wondering if the same applies to thread pooling.</p>
  
  <p>I'd benchmark it, if I knew what to measure, but my fear is that the problems may be more subtle than can be measured with a profiler.</p>
  
  <p>Note: the wisdom of using thread locals is not the issue here, so please don't suggest that I not use them.</p>