读读ThreadLocal

2019-01-31  本文已影响0人  传葱

前言

使用场景

public class AccessContext {
    private static final ThreadLocal<String> REQUEST_IP = new ThreadLocal<>();
    public static void setRequestIp(String ip) {
        REQUEST_IP.set(ip);
    }

    public static String getRequestIp() {
        return REQUEST_IP.get();
    }

    public static void clearAll() {
        REQUEST_IP.remove();
    }
}

原理

ThreadLocalMap

image.png
上一篇 下一篇

猜你喜欢

热点阅读