title: NullPointerException may related to InheritableThreadLocal
body: <p>I want to use InheritableThreadLocal to store some variables. So I wrote some code like this:</p>
  
  <pre><code>public class ThreadContext
  {
      private static ThreadLocal current = new InheritableThreadLocal();
  
      public static HashMap getContext()
      {
          if (current.get() == null) {
              createContext();
          }
          return (HashMap) current.get();
      }
  
      public static void createNewContext(){
          createContext();
      }
  
      public static IClientContext getClientContext()
      {
          return (IClientContext) ThreadContext.getContext().get("CLIENT_CONTEXT");
      }
  
      public static void setClientContext(IClientContext ctx) {
          ThreadContext.getContext().put("CLIENT_CONTEXT", ctx);
      }
  
      private static void createContext()
      {
          current.set(new HashMap());
      }
  }
  </code></pre>
  
  <p>But when other code call getClientContext, NullPointerException happens occasionally:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.xxx.util.ThreadContext.getClientContext(ThreadContext.java:19)
  </code></pre>
  
  <p>It looks like getContext returned a null value. But in getContext, it can not return null.
  Because if get returns null, it will create a new one.</p>
  
  <pre><code>public static HashMap getContext()
  {
      if (current.get() == null) {
          createContext();
      }
      return (HashMap) current.get();
  }
  </code></pre>
  
  <p>Any one met this problem before? Or any ideas?</p>