TreadLocal使用与分析

作用及注意事项        

        ThreadLocal的主要作用是提供线程内部的局部变量。这种变量在多线程环境下访问时(通过get或set方法访问),能保证各个线程里的变量相对独立于其他线程内的变量。每个线程通过其内部的ThreadLocalMap持有该线程本地实例的引用,因此ThreadLocal确保线程内的局部变量对其他线程是不可见的,实现了线程间的数据隔离。

        需要注意的是,ThreadLocal并不是为了解决多线程程序的并发问题或共享问题而设计的,而是提供了一种在线程内部存储和获取数据的方式,使得线程内的数据对其他线程不可见,从而实现线程间的数据隔离。

        此外,使用ThreadLocal时需要注意内存泄漏的问题,特别是在使用了线程池等场景时,由于线程是长生命周期的,如果每个线程内部都通过ThreadLocal持有了大对象,那么可能会导致内存溢出。因此,在使用完ThreadLocal后,最好手动调用其remove()方法,以显式删除线程局部变量,防止内存泄漏。

代码分析

使用代码演示功能:

package com.dream.test.threadLocal;

import java.text.SimpleDateFormat;
import java.util.Random;

public class ThreadLocalExample implements Runnable{

    private static final ThreadLocal<SimpleDateFormat>
//            threadLocal = ThreadLocal.withInitial(()-> new SimpleDateFormat("yyyy-MM-dd"));
    threadLocalNew = new ThreadLocal<>();

    private static final ThreadLocal<SimpleDateFormat>
            threadLocalInit = ThreadLocal.withInitial(()-> new SimpleDateFormat("yyyy-MM-dd"));


    public static void main(String[] args) throws InterruptedException {
        ThreadLocalExample example = new ThreadLocalExample();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(example, "thread:" + i);
            Thread.sleep(new Random().nextInt(100));
            thread.start();
        }
    }
    @Override
    public void run() {
        System.out.println("Thread Name= "+Thread.currentThread().getName()+" threadLocalNew 方式:"+ (threadLocalNew.get() == null ? "null" : threadLocalNew.get().toPattern()));
        System.out.println("Thread Name= "+Thread.currentThread().getName()+" threadLocalInit 方式: "+threadLocalInit.get().toPattern());
        try {
            Thread.sleep(new Random().nextInt(1000));
            threadLocalNew.set(new SimpleDateFormat());
            threadLocalInit.set(new SimpleDateFormat());
            System.out.println("Thread Name= "+Thread.currentThread().getName()+" reset threadLocalNew 方式:"+ (threadLocalNew.get() == null ? "null" : threadLocalNew.get().toPattern()));
            System.out.println("Thread Name= "+Thread.currentThread().getName()+" reset threadLocalInit 方式: "+threadLocalInit.get().toPattern());

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            // 清除当前线程的ThreadLocalMap中ThreadLocal的值
            threadLocalNew.remove();
            threadLocalInit.remove();
            // 重新获取查看是否已经删除
            System.out.println("Thread Name= "+Thread.currentThread().getName()+" remove threadLocalNew 方式:"+ (threadLocalNew.get() == null ? "null" : threadLocalNew.get().toPattern()));
            System.out.println("Thread Name= "+Thread.currentThread().getName()+" remove threadLocalInit 方式: "+threadLocalInit.get().toPattern());

        }
      }
}

程序运行结果:

通过运行结果,我们发现通过ThreadLocal.withInitial()方法创建的ThreadLocal对象remove没有清空值。这个问题通过ThreadLocal的get()来分析,我们先看set()方法。

ThreadLocal类set方法:

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



    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

    void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }

set方法作用是将值塞到当前线程Thead的TreadLocalMap中,其中key是TreadLocal类的弱引用。

TreadLocal的get()方法:

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

    private T setInitialValue() {
        T value = initialValue();
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
        return value;
    }

    protected T initialValue() {
        return null;
    }


    static final class SuppliedThreadLocal<T> extends ThreadLocal<T> {

        private final Supplier<? extends T> supplier;

        SuppliedThreadLocal(Supplier<? extends T> supplier) {
            this.supplier = Objects.requireNonNull(supplier);
        }

        @Override
        protected T initialValue() {
            return supplier.get();
        }
    }

get方法是从当前Thread的ThreadLocalMap中取值。

remove方法打印不一致问题,是因为remove后 ThreadLocalMap.Entry e 是null,程序会运行

setInitialValue方法,

这个方法会将 initialValue()方法的值重新塞进ThreadLocalMap中。通过new ThreadLocal<>()创建的TheadLocal对象运行ThreadLocal的initialValue()方法,返回null; 通过 ThreadLocal.withInitial()创建的对象运行的是 内部类SuppliedThreadLocal的initialValue()方法,返回的是初始值。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值