ThreadLocalUtil

发布时间:2024年01月03日



import lombok.Getter;

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

public class ThreadLocalUtil {

    private static final ThreadLocal<Map<ThreadLocalUtil.Key, Object>> CACHE = new ThreadLocal<Map<ThreadLocalUtil.Key, Object>>() {
        protected Map<ThreadLocalUtil.Key, Object> initialValue() {
            return new HashMap();
        }
    };

    public ThreadLocalUtil() {
    }

    public static Object put(String key, Object value) {
        return ((Map)CACHE.get()).put(key, value);
    }

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

    public static <T> T get(String key, Class<T> valueClass) {
        return valueClass.cast(get(key));
    }

    public static void remove() {
        CACHE.remove();
    }

    public static void clear() {
        ((Map)CACHE.get()).clear();
    }


    @Getter
    public static enum Key {
        ENABLE_POOLING;

        public static String getKey(String str) {
            Thread thread = Thread.currentThread();
            return thread + "_" + str;
        }
    }

}
ThreadLocalUtil.put(ThreadLocalUtil.Key.getKey(siteOrderNo),salesOrdersFetch);

? 记得使用

ThreadLocalUtil.remove();
文章来源:https://blog.csdn.net/u012240455/article/details/127768498
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。