title: ThreadLocal vs Parameter Passing - What to choose at Interface Design?
body: <p>I have already read the interesting discussion on following SO thread about ThreadLocal and its use. </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/817856/when-and-how-should-i-use-a-threadlocal-variable">When and how should I use a ThreadLocal variable?</a></li>
  <li><a href="https://stackoverflow.com/questions/1490919/purpose-of-threadlocal">Purpose of ThreadLocal?</a></li>
  <li><a href="https://stackoverflow.com/questions/11283369/how-does-threadlocal-usage-reduce-reusability">How does ThreadLocal usage reduce reusability</a></li>
  <li><a href="https://stackoverflow.com/questions/16839174/is-it-ok-to-use-threadlocal-for-storing-the-requested-locale">Is it OK to use ThreadLocal for storing the requested Locale?</a></li>
  </ul>
  
  <p>This questions is more towards a design time choice. My scenario is like this </p>
  
  <p>If I have a value object in a web application that may need to used inside same thread by almost all steps. I can think of two interface design options like below</p>
  
  <p><strong>Approach #1</strong> Using Method Parameter Passing.</p>
  
  <p>I have so far focused on coming up with a interface that can have methods with parameters of a value object interface. </p>
  
  <p>For example: </p>
  
  <pre><code>public interface SomeDataProcessorInterface {
  
  public void processSomething(SomeValueObjectInterface vo);
  
  }
  
  public interface SomeValueObjectInterface extends Serializable {}
  </code></pre>
  
  <p><strong>Aproach #2</strong> Using ThreadLocal </p>
  
  <p>In this approach I can have a interface with no method parameter and just create a static class to access my value objects using a threadlocal. </p>
  
  <p>For example:</p>
  
  <pre><code>public interface SomeDataProcessorInterface {
  
  public void processSomething();
  
  }
  
  public interface SomeValueObjectInterface extends Serializable {}
  
  public Class StaticClass {
  
      private static ThreadLocal&lt;SomeValueObjectInterface&gt; threadLocalVO = new ThreadLocal&lt;SomeValueObjectInterface&gt;();
  
       public static ThreadLocal getThreadLocal() {
          return threadLocal;
        }
  </code></pre>
  
  <p><strong>Which approach is better? and why?</strong></p>
  
  <p><strong>Which one of these implementation is going to have less chances of memory leak?</strong></p>
  
  <p><strong>Which one of these implementation is going to be good for Java Garbage Collector?</strong></p>
  
  <p>I read thru some of the points in other thread however I am still not clear which approach is better if I am starting from scratch.</p>