title: Extending java's ThreadLocal to allow the values to be reset across all threads
body: <p>After looking at <a href="https://stackoverflow.com/questions/2795447/is-there-no-way-to-iterate-over-or-copy-all-the-values-of-a-java-threadlocal" title="this question">this question</a>, I think I want to wrap ThreadLocal to add a reset behavior.</p>
  
  <p>I want to have something similar to a ThreadLocal, with a method I can call from any thread to set all the values back to the same value. So far I have this:</p>
  
  <pre><code>public class ThreadLocalFlag {
  
      private ThreadLocal&lt;Boolean&gt; flag;
      private List&lt;Boolean&gt; allValues = new ArrayList&lt;Boolean&gt;();
  
      public ThreadLocalFlag() {
          flag = new ThreadLocal&lt;Boolean&gt;() {
              @Override protected Boolean initialValue() {
                  Boolean value = false;
                  allValues.add(value);
                  return value;
              }
          };
      }
  
      public boolean get() {
          return flag.get();
      }
  
      public void set(Boolean value) {
          flag.set(value);
      }
  
      public void setAll(Boolean value) {
          for (Boolean tlValue : allValues) {
              tlValue = value;
          }
      }
  }
  </code></pre>
  
  <p>I'm worried that the autoboxing of the primitive may mean the copies I've stored in the list will not reference the same variables referenced by the ThreadLocal if I try to set them. I've not yet tested this code, and with something tricky like this I'm looking for some expert advice before I continue down this path.</p>
  
  <p>Someone will ask "Why are you doing this?". I'm working in a framework where there are other threads that callback into my code, and I don't have references to them. Periodically I want to update the value in a ThreadLocal variable they use, so performing that update requires that the thread which uses the variable do the updating. I just need a way to notify all these threads that their ThreadLocal variable is stale.</p>
  
  <hr>
  
  <p>I'm flattered that there is new criticism recently regarding this three year old question, though I feel the tone of it is a little less than professional. The solution I provided has worked without incident in production during that time. However, there are bound to be better ways to achieve the goal that prompted this question, and I invite the critics to supply an answer that is clearly better. To that end, I will try to be more clear about the problem I was trying to solve.</p>
  
  <p>As I mentioned earlier, I was using a framework where multiple threads are using my code, outside my control. That framework was QuickFIX/J, and I was implementing the <a href="http://www.quickfixj.org/quickfixj/javadoc/1.5.3/quickfix/Application.html" rel="nofollow noreferrer" title="Application interface">Application interface</a>. That interface defines hooks for handling FIX messages, and in my usage the framework was configured to be multithreaded, so that each FIX connection to the application could be handled simultaneously.</p>
  
  <p>However, the QuickFIX/J framework only uses a single instance of my implementation of that interface for all the threads. I'm not in control of how the threads get started, and each is servicing a different connection with different configuration details and other state. It was natural to let some of that state, which is frequently accessed but seldom updated, live in various <code>ThreadLocal</code>s that load their initial value once the framework has started the thread.</p>
  
  <p>Elsewhere in the organization, we had library code to allow us to register for callbacks for notification of configuration details that change at runtime. I wanted to register for that callback, and when I received it, I wanted to let all the threads know that it's time to reload the values of those <code>ThreadLocal</code>s, as they may have changed. That callback comes from a thread I don't control, just like the QuickFIX/J threads.</p>
  
  <p>My solution below uses <code>ThreadLocalFlag</code> (a wrapped <code>ThreadLocal&lt;AtomicBoolean&gt;</code>) solely to signal the other threads that it may be time to update their values. The callback calls <code>setAll(true)</code>, and the QuickFIX/J threads call <code>set(false)</code> when they begin their update. I have downplayed the concurrency issues of the <code>ArrayList</code> because the only time the list is added to is during startup, and my use case was smaller than the default size of the list.</p>
  
  <p>I imagine the same task could be done with other interthread communication techniques, but for what it's doing, this seemed more practical. I welcome other solutions.</p>