Java线程和多线程(七)——ThreadLocal

Java中的ThreadLocal是用来创建线程本地变量用的。我们都知道,访问某个对象的所有线程都是能够共享对象的状态的,所以这个对象状态就不是线程安全的。开发者可以通过使用同步来保证线程安全,但是如果不希望使用同步的话,我们也可以使用ThreadLocal变量。

Java ThreadLocal

其实每个线程都有自己的ThreadLocal变量,并且这个变量可以通过get()set()方法来获取默认值,或者修改其值。

ThreadLocal实例可以配置为静态私有变量来关联线程的状态。

Java ThreadLocal举例

下面的例子展示了在Java程序中如何使用ThreadLocal,也同时证实了,线程中都保留一份ThreadLocal的拷贝的。

package com.sapphire.threads;

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

public class ThreadLocalExample implements Runnable{

    // SimpleDateFormat is not thread-safe, so give one to each thread
    // SimpleDateFormat is not thread-safe, so give one to each thread
    private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
        @Override
        protected SimpleDateFormat initialValue()
        {
            return new SimpleDateFormat("yyyyMMdd HHmm");
        }
    };

    public static void main(String[] args) throws InterruptedException {
        ThreadLocalExample obj = new ThreadLocalExample();
        for(int i=0 ; i<10; i++){
            Thread t = new Thread(obj, ""+i);
            Thread.sleep(new Random().nextInt(1000));
            t.start();
        }
    }

    @Override
    public void run() {
        System.out.println("Thread Name= "+Thread.currentThread().getName()+" default Formatter = "+formatter.get().toPattern());
        try {
            Thread.sleep(new Random().nextInt(1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        formatter.set(new SimpleDateFormat());

        System.out.println("Thread Name= "+Thread.currentThread().getName()+" formatter = "+formatter.get().toPattern());
    }
}

上面程序的输出结果类似下面的结果:

Thread Name= 0 default Formatter = yyyyMMdd HHmm
Thread Name= 1 default Formatter = yyyyMMdd HHmm
Thread Name= 0 formatter = M/d/yy h:mm a
Thread Name= 2 default Formatter = yyyyMMdd HHmm
Thread Name= 1 formatter = M/d/yy h:mm a
Thread Name= 3 default Formatter = yyyyMMdd HHmm
Thread Name= 4 default Formatter = yyyyMMdd HHmm
Thread Name= 4 formatter = M/d/yy h:mm a
Thread Name= 5 default Formatter = yyyyMMdd HHmm
Thread Name= 2 formatter = M/d/yy h:mm a
Thread Name= 3 formatter = M/d/yy h:mm a
Thread Name= 6 default Formatter = yyyyMMdd HHmm
Thread Name= 5 formatter = M/d/yy h:mm a
Thread Name= 6 formatter = M/d/yy h:mm a
Thread Name= 7 default Formatter = yyyyMMdd HHmm
Thread Name= 8 default Formatter = yyyyMMdd HHmm
Thread Name= 8 formatter = M/d/yy h:mm a
Thread Name= 7 formatter = M/d/yy h:mm a
Thread Name= 9 default Formatter = yyyyMMdd HHmm
Thread Name= 9 formatter = M/d/yy h:mm a

从代码中我们可以看到,10个线程都共享同一个对象,引用的是同一个ThreadLocal<SimpleDateFormat> formatter,看上面的代码,当线程0执行了formatter.set(new SimpleDateFormat())的时候,显然,读取的线程2的formatter仍然是默认的formatter,说明修改公共的formatter其实并没有生效,从每个线程单独来看,也没有破坏线程的安全性。

ThreadLocal原理

到了这里,很多人会奇怪,ThreadLocal的实现方式,下面我们来看下ThreadLocal的实现方案,首先看下这个其set和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) {
                @SuppressWarnings("unchecked")
                T result = (T)e.value;
                return result;
            }
        }
        return setInitialValue();
    }

    /**
     * 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);
    }

get和set方法中都有一个核心的概念,就是ThreadLocalMap其实,这个Map是根据线程绑定的,参考如下代码:

    /**
     * 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;
    }

上面的代码是ThreadLocal中的getMap(Thread t)方法,这个方法来返回绑定到线程上的线程本地变量。线程的内部其实都会维护ThreadLocalMap的。通过前面的set和get方法,那么我们就知道ThreadLocal的实现方案了。ThreadLocalMap本质上,是一个HashMap,从线程到类型T的一个映射。这也就解释了,为什么我们将ThreadLocal定义为static final仍然不会影响线程的安全,因为我们之前代码中访问到的formatter其实都已经扔到了ThreadLocalMap里面,这样,每次调用get,其实会通过Thread.currentThread()找到对应的ThreadLocalMap,进而找到对应的formmater副本,调用set方法改变的都是ThreadLocalMap里面的值,自然就不会影响到我们在ThreadLocalExample之中的formatter变量,自然也就不存在线程安全问题。

同时,这也解释了我们为什么ThreadLocal的变量定义为了static final的,因为就算定义为非static的,仍然是没有任何意义的,只会增加额外的内存而已,因为我们本质上修改的不是ThreadLocalExample中的实例,而是ThreadLocalMap中的副本,所以定义为static final正合适。

ThreadLocal本质上其实是将一些变量副本写入Thread当中的,所以内存占用会更大,开发者可以根据自己的需求考虑是通过同步或者ThreadLocal的方式来实现线程安全操作。

原文地址:https://www.cnblogs.com/qitian1/p/6461527.html