Get the value of the key set from the ThreadLocal instance - Java java.lang

Java examples for java.lang:ThreadLocal

Description

Get the value of the key set from the ThreadLocal instance

Demo Code


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";
        System.out.println(get(key));
    }/*from  ww w  .  j a va  2  s  .  c o  m*/
    private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();
    /**
     * 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);
    }
}

Related Tutorials