title: Questions about using ThreadLocal in a Spring singleton scoped service
body: <p>In my singleton scoped service class below, all methods in the class require some user context that is known when <code>Service.doA()</code> is called. Instead of passing around info across methods, I was thinking about storing those values in <code>TheadLocal</code>. I have two questions about this approach:</p>
  
  <p>1) Does the implementation below use <code>ThreadLocal</code> correctly? That is, it is thread-safe and the correct values will be read/written into the <code>ThreadLocal</code>?</p>
  
  <p>2) Does <code>ThreadLocal userInfo</code> need to be cleaned up explicitly to prevent any memory leaks? Will it be garbage collected? </p>
  
  <pre><code>@Service
  public class Service {
      private static final ThreadLocal&lt;UserInfo&gt; userInfo = new ThreadLocal&lt;&gt;(); 
  
      public void doA() {
          // finds user info
          userInfo.set(new UserInfo(userId, name));
          doB();
          doC();
      }
  
      private void doB() {
          // needs user info
          UserInfo userInfo = userInfo.get();
      }
  
      private void doC() {
          // needs user info
          UserInfo userInfo = userInfo.get();
      }
  }
  </code></pre>