title: Why should I avoid using InheritableThreadLocal in servlets?
body: <p>I am using <code>InheritableThreadLocal</code> in my <code>Servlet</code> Class. So that It will available from it's child threads. Is that evil using <code>InheritableThreadLocal</code> in thread pool executors? . Such as servlet thread pool. </p>
  
  <p>My Questions.</p>
  
  <p>1) Why should we avoid using <code>InheritableThreadLocals</code> in servlets?</p>
  
  <p>2) Is this memory leak possible in <code>InheritableThreadLocal</code>?</p>
  
  <p>3) Is there any alternative for <code>InheritableThreadLocal</code>?.</p>
  
  <p>4) What happens if the thread will be reused , the value stored in <code>threadlocal</code> will not be cleared?</p>
  
  <p><strong>My Real Time Scenario</strong></p>
  
  <pre><code>public class UserAccessFilter implements javax.servlet.Filter {
  
        static final InheritableThreadLocal&lt;String&gt; currentRequestURI = new InheritableThreadLocal&lt;String&gt;();
  
        public void  doFilter(ServletRequest req, ServletResponse resp , FilterChain fc) throws IOException, ServletException{
                String uri = request.getRequestURI();
                fc.doFilter(request, response);         
        }
  }
  
  
  public class MailServlet extends HttpServlet{
  
        @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      String mailCount = req.getParameter("mailCount");
  
      if(mailCount != null &amp;&amp; !"".equals(mailCount) &amp;&amp; mailCount.matches("[0-9]+")){
          MailThread mailThread = new MailThread("xxx@gmail.com", generateToAddress(Integer.parseInt(mailCount))); //NO I18N
          Thread t = new Thread(mailThread);
          t.start();
      }
  
      resp.getWriter().println("Mail Servlet.............."); //NO I18N
  
  }              
  
  }
  
  class MailThread implements Runnable{
  
  private String from;
  private String to;
  
  public MailThread(String from , String to){
      this.from = from;
      this.to = to;
  }
  
  
  @Override
  public void run() {
      sendMail();
  }
  
      public void sendMail(){
          //I want this uri from child threads. I can't pass this value to this constructor.
          String uri = currentRequestURI.get();
          //Do Mail Operations
      }
  
  
  }
  </code></pre>
  
  <p>Filter  --> Servlet A --> Child Thread ---> Mail Thread (Here I am getting the value setted in filter) .</p>