ThreadLocal原理与模拟

 

首先用一个程序模拟一下ThreadLocal:

public class ThreadLocal1 {
    private static Dictionary<Thread, Integer> map;

    public static void main(String[] args) {
        map = new Hashtable<Thread, Integer>();
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {

                public void run() {
                    int data = new Random().nextInt();
                    map.put(Thread.currentThread(), data);
                    System.out.println(Thread.currentThread() + ", data:"
                            + data);
                    new A().show();
                    new B().show();
                }
            }).start();
        }
    }

    static class A {
        public void show() {
            System.out.println(Thread.currentThread() + "调用A, data:" + map.get(Thread.currentThread()));
        }
    }

    static class B {
        public void show() {
            System.out.println(Thread.currentThread() + "调用B, data:" + map.get(Thread.currentThread()));
        }
    }
}

 

运行结果:

Thread[Thread-1,5,main], data:1170863694
Thread[Thread-0,5,main], data:1982496284
Thread[Thread-1,5,main]调用A, data:1170863694
Thread[Thread-0,5,main]调用A, data:1982496284
Thread[Thread-0,5,main]调用B, data:1982496284
Thread[Thread-1,5,main]调用B, data:1170863694

接着,使用ThreadLocal来写一个类似的例子:

public class ThreadLocal2 {

    public static void main(String[] args) {
        for (int i = 0; i < 2; i++) {
            new Thread(new Runnable() {

                public void run() {
                    int data = new Random().nextInt(1000);
                    Student instance = Student.getInstance();
                    instance.setName(data + "");
                    instance.setAge(data);
                    System.out.println(Thread.currentThread() + ", name:"
                            + instance.getName() + ",age:" + instance.getAge());
                    new A().show();
                    new B().show();
                }
            }).start();
        }
    }

    static class A {
        public void show() {
            Student instance = Student.getInstance();
            System.out.println(Thread.currentThread() + "调用A, name:"
                    + instance.getName()+",age:"+instance.getAge());
        }
    }

    static class B {
        public void show() {
            Student instance = Student.getInstance();
            System.out.println(Thread.currentThread() + "调用B, name:"
                    + instance.getName()+",age:"+instance.getAge());
        }
    }
}

class Student {

    // private Student instance;
    private static ThreadLocal<Student> threadInstance = new ThreadLocal<Student>();

    private Student() {
    }

    public static Student getInstance() {
    
        Student instance = threadInstance.get();
        if (instance == null) {
            instance = new Student();
            threadInstance.set(instance);
        }
        return instance;
    }

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

}

 

运行结果:

Thread[Thread-0,5,main], name:568,age:568
Thread[Thread-1,5,main], name:388,age:388
Thread[Thread-1,5,main]调用A, name:388,age:388
Thread[Thread-0,5,main]调用A, name:568,age:568
Thread[Thread-0,5,main]调用B, name:568,age:568
Thread[Thread-1,5,main]调用B, name:388,age:388

我们可以看到,以上程序中,当使用ThreadLocal维护变量时,ThreadLocal为每个使用该变量的线程提供独立的变量副本,所以每一个线程都可以独立地改变自己的副本,而不会影响其它线程所对应的副本。

但这其中一共有两个问题值得思考:

1、每个线程的变量副本是存储在哪里的?

2、变量副本是怎么从共享的那个变量赋值出来的?源码中的threadlocal的初始值是什么时机设置的?

=====================================

最关键的问题是:ThreadLocal是怎么实现了多个线程之间每个线程一个变量副本的?它是如何实现共享变量的。

 

ThreadLocal提供了set和get访问器用来访问与当前线程相关联的线程局部变量。

 

可以从ThreadLocal的get函数中看出来,其中getmap函数是用t作为参数,这里t就是当前执行的线程。

 

从而得知,get函数就是从当前线程的threadlocalmap中取出当前线程对应的变量的副本【注意,map是保存在线程中的,而不是保存在ThreadLocal变量中】当前线程中,有一个变量引用名字是threadLocals,这个引用是在ThreadLocal类中createmap函数内初始化的。每个线程都有一个这样的threadLocals引用的ThreadLocalMap,以ThreadLocal和ThreadLocal对象声明的变量类型作为参数。这样,我们所使用的ThreadLocal变量的实际数据,通过get函数取值的时候,就是通过取出Thread中threadLocals引用的map,然后从这个map中根据当前threadLocal作为参数,取出数据。现在,变量的副本从哪里取出来的(本文章提出的第一个问题)已经确认解决了。

 

【ThreadLocal整体上给我的感觉就是,一个包装类。声明了这个类的对象之后,每个线程的数据其实还是在自己线程内部通过threadLocals引用到的自己的数据。只是通过ThreadLocal访问这个数据而已】

ThreadLocalMap确实是ThreadLocal中的静态内部类,但Thread中局部对象包括这个ThreadLocalMap。

public class Thread implements Runnable {
    ......

    /* ThreadLocal values pertaining to this thread. This map is maintained
     * by the ThreadLocal class. */
    ThreadLocal.ThreadLocalMap threadLocals = null;  
    ......
}

 
ThreadLocal 的工作原理在于:每个Thread调用到ThreadLocal 的set ( T )方法时,其实内部实现的就是ThreadLocal 对象创建一个ThreadLocalMap对象塞到调用的那个Thread局部对象中。真正保存的数值都存在每个Thread自己那边,当然线程之间没什么影响啊。

说明下ThreadLocalMap 中保存的值:类似于 Map(key,value) 保存的,key=ThreadLocal 对象,value=你传递的值对象。 这个ThreadLocalMap 保存在每个调用的Thread那边!

 

=================================

 

那么还剩下第二个问题。变量副本是什么时候“复制”到threadlocal中的呢?这里“复制”两个字用的很不专业。准确的说,应该是,变量副本【每个线程中保存的那个map中的变量】是怎么声明和初始化的?

 

看下面ThreadLocal类中的set函数的源码:

 /**
     * Sets the current thread's copy of this thread-local variable
     * to the specified value.  Most subclasses will have no need to
     * override this method, relying solely on the {@link #initialValue}
     * method to set the values of thread-locals.
     *
     * @param value the value to be stored in the current thread's copy of
     *        this thread-local.
     */
    public void set(T value) {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null)
            map.set(this, value);
        else
            createMap(t, value);
    }

 

 再来看getMap()和createMap()函数的源码:

/**
     * Get the map associated with a ThreadLocal. Overridden in
     * InheritableThreadLocal.
     *
     * @param  t the current thread
     * @return the map
     */
    ThreadLocalMap getMap(Thread t) {
        return t.threadLocals;
    }

    /**
     * Create the map associated with a ThreadLocal. Overridden in
     * InheritableThreadLocal.
     *
     * @param t the current thread
     * @param firstValue value for the initial entry of the map
     * @param map the map to store.
     */
    void createMap(Thread t, T firstValue) {
        t.threadLocals = new ThreadLocalMap(this, firstValue);
    }

 

下面再来看一下ThreadLocal类中的get()函数的源码:

 /**
     * Returns the value in the current thread's copy of this
     * thread-local variable.  If the variable has no value for the
     * current thread, it is first initialized to the value returned
     * by an invocation of the {@link #initialValue} method.
     *
     * @return the current thread's value of this thread-local
     */
    public T get() {
        Thread t = Thread.currentThread();
        ThreadLocalMap map = getMap(t);
        if (map != null) {
            ThreadLocalMap.Entry e = map.getEntry(this);
            if (e != null)
                return (T)e.value;
        }
        return setInitialValue();
    }

 

当线程中的threadlocalmap是null的时候,会调用createmap创建一个map。同时根据函数参数设置上初始值。也就是说,当前线程的threadlocalmap是在第一次调用set的时候创建map并且设置上相应的值的。

 

解释如下:

 

1、在代码中声明的ThreadLocal对象,实际上只有一个。

 

2、在每个线程中,都维护了一个threadlocals对象,在没有ThreadLocal变量的时候是null的。一旦在ThreadLocal的createMap函数中初始化之后,这个threadlocals就初始化了。以后每次那个ThreadLocal对象想要访问变量的时候,比如set函数和get函数,都是先通过getMap(t)函数,先将线程的map取出,然后再从这个在线程(Thread)中维护的map中取出数据【以当前threadlocal作为参数】。

 

到此,第二个问题也解决了。

 

 

从这个函数中可以看出来,Thread中的threadlocals变量是在ThreadLocal对象中调用createMap函数来初始化的。其实在Thread的代码中可以搜搜看,是没有threadlocals这个变量的很多应用场景的。主要就是用在ThreadLocal中用来set和get函数中。

 

———————————————————-

 

那么上面的问题解决之后,又来了一个问题。不同的线程局部变量,比如说声明了n个(n>=2)这样的线程局部变量threadlocal,那么在Thread中的threadlocals中是怎么存储的呢?threadlocalmap中是怎么操作的?

 

在ThreadLocal的set函数中,可以看到,其中的map.set(this, value);把当前的threadlocal传入到map中作为键,也就是说,在不同的线程的threadlocals变量中,都会有一个以你所声明的那个线程局部变量threadlocal作为键的key-value。假设说声明了N个这样的线程局部变量变量,那么在线程的ThreadLocalMap中就会有n个分别以你的线程局部变量作为key的键值对。

 

———————————————————-

 

至此,所有的关于threadlocal的问题都已经解决了。

 

转载于:https://www.cnblogs.com/DarrenChan/p/5774929.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值