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

com.spring.boxes.dollar.ThreadLocalUtils Maven / Gradle / Ivy

package com.spring.boxes.dollar;

import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public class ThreadLocalUtils {

    private static final TransmittableThreadLocal> THREAD_LOCAL_MAP =
            new TransmittableThreadLocal>() {
                @Override
                protected Map initialValue() {
                    return new ConcurrentHashMap<>(16);
                }
            };

    /**
     * 设置一个值到ThreadLocal
     *
     * @param key   键
     * @param value 值
     * @param    值的类型
     * @return 被放入的值
     * @see Map#put(Object, Object)
     */
    public static  T put(String key, T value) {
        THREAD_LOCAL_MAP.get().put(key, value);
        return value;
    }

    /**
     * 删除参数对应的值
     *
     * @param key
     * @see Map#remove(Object)
     */
    public static void remove(String key) {
        THREAD_LOCAL_MAP.get().remove(key);
    }

    /**
     * 清空ThreadLocal
     *
     * @see Map#clear()
     */
    public static void clear() {
        THREAD_LOCAL_MAP.remove();
    }

    /**
     * 从ThreadLocal中获取值
     *
     * @param key 键
     * @param  值泛型
     * @return 值, 不存在则返回null, 如果类型与泛型不一致, 可能抛出{@link ClassCastException}
     * @see Map#get(Object)
     * @see ClassCastException
     */
    @SuppressWarnings("unchecked")
    public static  T get(String key) {
        return ((T) THREAD_LOCAL_MAP.get().get(key));
    }

    /**
     * 从ThreadLocal中获取值,并指定一个当值不存在的提供者
     *
     * @see Supplier
     * @since 3.0
     */
    @SuppressWarnings("unchecked")
    public static  T get(String key, Supplier supplierOnNull) {
        return ((T) THREAD_LOCAL_MAP.get().computeIfAbsent(key, k -> supplierOnNull.get()));
    }

    public static  T getAndRemove(String key) {
        try {
            return get(key);
        } finally {
            remove(key);
        }
    }

    public static TransmittableThreadLocal> getThreadLocalMap() {
        return THREAD_LOCAL_MAP;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy