title: Java - ThreadLocal or Concurrent Object Pool?
body: <p>Which method is more acceptable to access to a <strong>Non Thread Safe Object</strong> ?</p>
  
  <p>using <code>ThreadLocal</code> objects :</p>
  
  <pre><code>static final ThreadLocal&lt;NonThreadSafeParser&gt; PARSER_THREAD_LOCAL = new ThreadLocal&lt;NonThreadSafeParser&gt;() {
      @Override
      protected NonThreadSafeParser initialValue() {
          return new NonThreadSafeParser();
      }
  };
  
  void parse(String input) {
      PARSER_THREAD_LOCAL.get().parse(input);
  }
  </code></pre>
  
  <p>using a <strong>Concurrent Object Pool</strong> :</p>
  
  <pre><code>static final ConcurrentObjectPool&lt;NonThreadSafeParser&gt; PARSER_POOL = new ConcurrentObjectPool&lt;&gt;();
  
  void parse(String input) {
      NonThreadSafeParser parser = PARSER_POOL.borrow();
      try {
          parser.parse(input);
      } finally {
          PARSER_POOL.release(parser);
      }
  }
  </code></pre>
  
  <p>or other methods that you want to offer?</p>
  
  <p>Important factors are :</p>
  
  <ul>
  <li>Performance</li>
  <li>Memory Usage</li>
  <li>Garbage Collection</li>
  </ul>
  
  <p>Generally, what are the <strong>Pros</strong> and <strong>Cons</strong> for each method?</p>
  
  <p>Is there any noticeable difference between them?</p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong> :</p>
  
  <p>An example of <a href="https://github.com/EsotericSoftware/kryo/tree/master/src/com/esotericsoftware/kryo/pool" rel="nofollow"><strong>Concurrent Object Pool</strong></a>s used by <a href="https://github.com/EsotericSoftware/kryo#pooling-kryo-instances" rel="nofollow"><strong>Kryo</strong></a>.</p>