聊聊ThreadLocal

转载:https://mp.weixin.qq.com/s?__biz=MzkxNjQxODg1Mw==&mid=2247487427&idx=1&sn=035245fc459973c8be698d67ecf42d9c&chksm=c1517789f626fe9fe40f09e2bd96ff9cd82f86e875621f72c577650ece158160db2b5978c9c0&scene=132&exptype=timeline_recommend_article_extendread_samebiz#wechat_redirect

在这里插入图片描述

public class ThreadLocalDemo {
    private static Map<String, String> map = new HashMap<>();

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            int finalI = i;
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + ",value实际赋值为:" +finalI + "");
                //map变量赋值
                map.put("key", finalI + "");
                getValue();
            }).start();
        }
    }

    public static void getValue() {
        String value = map.get("key");
        System.out.println(Thread.currentThread().getName() + ",getValue获取值为:" + value + "");
    }
}

在这里插入图片描述

public class ThreadLocalDemo2 {
    private static ThreadLocal<Map<String, String>> map = new ThreadLocal<>();

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            int finalI = i;
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + ",value实际赋值为:" + finalI + "");
                //map变量赋值
                Map<String, String> stringMap = map.get();
                if (stringMap == null) {
                    stringMap = new HashMap<>();
                    map.set(stringMap);
                }
                stringMap.put("key", finalI + "");
                getValue();
            }).start();
        }
    }

    public static void getValue() {
        if (map.get() == null) {
            return;
        }
        String value = map.get().get("key");
        System.out.println(Thread.currentThread().getName() + ",getValue获取值为:" + value + "");
    }
}

在这里插入图片描述

public class ThreadLocalDemo3 {
    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            int finalI = i;
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + ",value实际赋值为:" +finalI + "");
                //map变量赋值
                Map<String, String> map = new HashMap<>();
                map.put("key", finalI + "");
                getValue(map);
            }).start();
        }
    }

    public static void getValue(Map<String, String> map) {
        String value = map.get("key");
        System.out.println(Thread.currentThread().getName() + ",getValue获取值为:" + value + "");
    }
}

在这里插入图片描述
在这里插入图片描述

   public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null) {
            map.set(this, value);
        } else {
            createMap(t, value);
        }
    }

在这里插入图片描述

int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);

在这里插入图片描述

private void set(ThreadLocal<?> key, Object value) {
        Entry[] tab = table;
        int len = tab.length;
        int i = key.threadLocalHashCode & (len-1);

        for (Entry e = tab[i];
             e != null;
             e = tab[i = nextIndex(i, len)]) {
                //略。。。
        }

        tab[i] = new Entry(key, value);
             //略。。。
        }

在这里插入图片描述

public T get() {
    Thread t = Thread.currentThread();
    ThreadLocalMap map = getMap(t);
    if (map != null) {
        ThreadLocalMap.Entry e = map.getEntry(this);
        if (e != null) {
            T result = (T)e.value;
            return result;
        }
    }
    return setInitialValue();
}

在这里插入图片描述

int i = key.threadLocalHashCode & (table.length - 1);

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值