title: Cleaning up InheritableThreadLocal when child threads are still running
body: <p>We are using <code>InheritableThreadLocal</code> in a web application at request level to store an <code>ApplicationContext</code> object that is used throughout the application.
  There is a scenario for an async response wherein the request creates an acknowledgement ID and launches a child thread and returns the acknowledgement ID back. Finally, the <code>ThreadLocal</code> is removed.</p>
  
  <p>However, the child thread seems to lose the ApplicationContext (is null) if it is running longer than the time it takes for the request to be processed. </p>
  
  <p>Pseudo code:</p>
  
  <pre><code>doPost() {
   ApplicationContext ctx = createApplicationContext(req);
   MyThreadLocalContainer.setContext(ctx);
   new Thread(new RequestProcessor()).start(); // this seems to lose the ctx 
   MyThreadLocalContainer.removeContext();
  }
  </code></pre>
  
  <p>Definition of MyThreadLocalContainer:</p>
  
  <pre><code>public class MyThreadLocalContainer {
   private static final ThreadLocal&lt;ApplicationContext&gt; APP_CTX = new InheritableThreadLocal&lt;ApplicationContext&gt;();
   public static void setContext(ApplicationContext ctx) {
     APP_CTX.set(ctx);
   }
  
   public static void removeContext() {
     APP_CTX.remove();
   }
  }
  </code></pre>
  
  <p>How can the <code>ThreadLocal</code> cleanup be handled in this scenario without having to wait for the child thread to complete before calling <code>removeContext()</code>?</p>