title: Java associate data with current thread
body: <p>I am working on web application, which is deployed on apache tomcat web server. I want to associate data with current thread of http request. For this purpose I have created following class:</p>
  
  <pre><code>public class ThreadData {
  
      private static final Map&lt;Long, Map&lt;String, String&gt;&gt; data = new HashMap&lt;&gt;();
  
      public static Map&lt;String, String&gt; getDataMap(long threadId) {
          if (data.get(threadId) == null) {
              data.put(threadId, new HashMap&lt;String, String&gt;());
          }
  
          return data.get(threadId);
      }
  
      public static void put(String key, String value) {
          long threadId = Thread.currentThread().getId();
          getDataMap(threadId).put(key, value);
      }
  
      public static String get(String key) {
          long threadId = Thread.currentThread().getId();
          return getDataMap(threadId).get(key);
      }
  }
  </code></pre>
  
  <p>I want to know if such kind of solution is right and safe for this problem .</p>