Java examples for java.lang:ThreadLocal
Set the on the ThreadLocal instance
import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpSession; public class Main{ public static void main(String[] argv) throws Exception{ String key = "java2s.com"; Object value = "java2s.com"; set(key,value);/* w ww. j a v a 2 s . c o m*/ } private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>(); /** * Set the <key,value> on the {@link ThreadLocal} instance * @param key * @param value */ public static void set(String key, Object value) { Map<String, Object> mapOfObjects = null; //this is the firstTime for this Thread! if (threadLocal.get() == null) { mapOfObjects = new HashMap<String, Object>(); threadLocal.set(mapOfObjects); } ((Map<String, Object>) threadLocal.get()).put(key, value); } /** * Get the value of the key set from the {@link ThreadLocal} instance * @param key * @return */ public static Object get(String key) { if (threadLocal.get() == null) { return null; } return ((Map<String, Object>) threadLocal.get()).get(key); } }