title: Using ThreadLocal in instance variables
body: <p><strong>Do Java <code>ThreadLocal</code> variables produce thread-local values if they are used as instance variables</strong> (e.g., in a method that generates  thread-local objects), or must they always be static to do so?</p>
  
  <p>As an example, assume a typical scenario where several, expensive to initialize objects of a class that is not thread-safe, need to be instantiated in a single static initialization block, stored in static variables of a single class (e.g., in a <code>Map</code> data structure) and from then on used for intensive processing by numerous different threads.</p>
  
  <p>To achieve thread safety, obviously a different copy of each static object must be passed. For instance, Java <code>DateFormat</code> objects that need to be safely used across different threads.</p>
  
  <p>In many examples one can find on the web, the approach seems to be declaring separately each <code>ThreadLocal</code> variable, instantiate the new object in the <code>initialValue()</code> method and then use the <code>get()</code> method to retrieve a thread-local instance.</p>
  
  <p>This approach is not very efficient if there are dozens or hundreds of such objects to be created, each with its own initialization parameters. For example, many <code>SimpleDateFormat</code> objects with a different date pattern each.</p>
  
  <p>If the instantiation of the objects could be done in a loop which produces a different value in each iteration, a generic method for producing the thread-local instances would be needed, after each value is created by properly initializing the corresponding object.</p>
  
  <p>Based on the above, the following generic static method would not work, because the same reference is produced on every call to initialValue():</p>
  
  <pre><code>// Each value is an object initialized prior to calling getLocal(...)
  public static final &lt;T&gt; T getLocal(final T value)
  {
      ThreadLocal&lt;T&gt; local = new ThreadLocal&lt;T&gt;()
      {
          @Override
          protected T initialValue()
          {
              return value;
          }
      };
  
      return local.get();
  }
  </code></pre>
  
  <p>Instead, a mechanism for creating a new object inside initialValue() is needed. So, the only generic approach is probably using reflection, in a pattern similar to</p>
  
  <pre><code>private static final &lt;T&gt; T getLocal(
          final Constructor&lt;T&gt; constructor, final Object[] initargs)
  {
      ThreadLocal&lt;T&gt; local = new ThreadLocal&lt;T&gt;()
      {           
          @Override
          protected T initialValue()
          {
              T value = null;
  
              try // Null if the value object cannot be created
              {
                  value = constructor.newInstance(initargs);
              }
              catch (Exception e)
              {
              }
  
              return value;
          }
      };
  
      return local.get();
  }
  </code></pre>
  
  <p>Then, of course, there is the type-specific option, where one could just use the <code>ThreadLocal</code> pattern in the loop for declaring each variable.</p>
  
  <p>For example, in the case of <code>DateFormat</code>, in a single, static initialization block, one could do</p>
  
  <pre><code>private static String[] patterns = ... // Get date patterns
  private static DateFormat format;
  
  public static Map&lt;String, DateFormat&gt; formats = new HashMap&lt;String, DateFormat&gt;();
  
  static
  {
      for (final String pattern:patterns)
      {
          format = new ThreadLocal&lt;DateFormat&gt;()
          {           
                  @Override
              protected DateFormat initialValue()
                  {
              return new SimpleDateFormat(pattern);
              }
          }.get();
  
          formats.put(pattern, format);
  }
  </code></pre>
  
  <p>From then on, the <code>formats</code> map will be read by different classes, across different threads, each time in order to invoke the <code>format()</code> or <code>parse()</code> method of one or more <code>DateFormat</code> objects stored in the map.</p>
  
  <p><strong>Does any of the above approaches make sense for the case described, or should the <code>ThreadLocal</code> declarations be static?</strong></p>