All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.azbh111.utils.java.context.ThreadLocalContext Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
package com.github.azbh111.utils.java.context;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author pyz
 * @date 2019/3/30 3:38 PM
 */
public class ThreadLocalContext {
    private static final ThreadLocal> threadLocal = new ThreadLocal<>();

    static {
        threadLocal.set(new HashMap<>());
    }

    public static Map clear() {
        Map map = threadLocal.get();
        threadLocal.remove();
        return map;
    }

    public static  T get(String key) {
        return (T) get().get(key);
    }


    public static void set(String key, Object value) {
        get().put(key, value);
    }

    public static Object remove(String key) {
        return get().remove(key);
    }

    public static Map get() {
        Map map = threadLocal.get();
        if (map == null) {
            map = new HashMap<>();
            threadLocal.set(map);
        }
        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy