set ThreadLocal - Java java.lang

Java examples for java.lang:ThreadLocal

Description

set ThreadLocal

Demo Code


//package com.java2s;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] argv) throws Exception {
        String key = "java2s.com";
        String value = "java2s.com";
        set(key, value);/*  www .j a v a2s  . c o  m*/
    }

    public static final ThreadLocal<Map<String, String>> threadLocals = new ThreadLocal<Map<String, String>>();

    public static void set(String key, String value) {
        Map<String, String> values = threadLocals.get();
        if (values == null) {
            values = new HashMap<String, String>();
            threadLocals.set(values);
        }
        values.put(key, value);
    }

    public static String get(String key) {
        Map<String, String> values = threadLocals.get();
        if (values == null) {
            return null;
        }
        return values.get(key);
    }
}

Related Tutorials