搜索
您的当前位置:首页正文

对ThreadLocal的认识

来源:二三娱乐

ThreadLocal的初步认识

在正式解析ThreadLocal之前,我们先来看 一个例子:

public class ThreadLocalTest {
    //创建并初始化一个ThreadLocal变量
    private static ThreadLocal<Integer> cnt = new ThreadLocal<Integer>(){
        @Override
        protected Integer initialValue() {
            return 0;
        }
    };

    public static void main(String[] args){
        System.out.println(Thread.currentThread().getName() + " before write, get cnt " + cnt.get());
        cnt.set(1);
        //创建线程一对ThreadLocal变量进行修改和读取
        new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " before write, get cnt : " + cnt.get());
                cnt.set(2);
                System.out.println(Thread.currentThread().getName() + " after write, get cnt : " + cnt.get());
            }
        }.start();
        //创建线程二对ThreadLocal变量进行修改和读取
        new Thread(){
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " before write, get cnt : " + cnt.get());
                cnt.set(3);
                System.out.println(Thread.currentThread().getName() + " get cnt : " + cnt.get());

            }
        }.start();
        //主线程进行读取
        System.out.println(Thread.currentThread().getName() + " after write, get cnt : " + cnt.get());
    }

}
输出结果:
main before write, get cnt 0
Thread-0 before write, get cnt : 0
Thread-0 after write, get cnt : 2
main after write, get cnt : 1
Thread-1 before write, get cnt : 0
Thread-1 get cnt : 3

程序中存在三个线程(包括主线程)对变量cnt进行修改并读取,根据结果,我们发现不同线程之间对变量的修改和读取都是独立的,互不影响的。实际上,这就是ThreadLocal的特色。

ThreadLocal在每个线程中对变量会创建一个副本,即每个线程内部都会有一个变量,且在线程内部任何地方可以使用,线程之间互不影响。

ThreadLocal的深入理解

ThreadLocal类主要提供以下几个方法:

public T get() {}
public void set(T value) {}
public void remove() {}
protected T initValue() {}
  • 我们先看到initValue方法:
protected T initialValue() {
     return null;
}

我们会发现这是个空方法,因此,我们在进行初始化时,必须重写该方法才能执行初始化操作。

  • 接下来,我们继续解析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();
}

首先通过Thread.currentThread()方法获取到当前线程,然后再根据当前线程调用getMap方法获取到ThreadLocalMap对象。那么我们来看看getMap方法:

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

我们可以看到它返回的是线程中的一个threadLocals变量,而我们在Thread类中,也可以发现这个变量:

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

若map不为空,那么将通过map.getEntry(this)方法来获取到Entry对象,我们来看看Entry对象到底是什么:

static class Entry extends WeakReference<ThreadLocal<?>> {
    /** The value associated with this ThreadLocal. */
    Object value;

    Entry(ThreadLocal<?> k, Object v) {
        super(k);
        value = v;
    }
}

也就是说,Entry对象是ThreadLocal的一个弱引用而已,这里应该是方便GC的回收,防止内存溢出。我们继续看到getEntry方法:

private Entry getEntry(ThreadLocal<?> key) {
    int i = key.threadLocalHashCode & (table.length - 1);
    Entry e = table[i];
    if (e != null && e.get() == key)
        return e;
    else
        return getEntryAfterMiss(key, i, e);
}

看到这里,可能就有些懵逼了,这里的threadLocalHashCodetable完全不知所云。这里,我们就要回头看ThreadLocal中的一些变量:

private final int threadLocalHashCode = nextHashCode();

/**
  * The next hash code to be given out. Updated atomically. Starts at
  * zero.
  */
private static AtomicInteger nextHashCode =
    new AtomicInteger();

/**
  *The difference between successively generated hash codes - turns
  * implicit sequential thread-local IDs into near-optimally spread
  * multiplicative hash values for power-of-two-sized tables.
  */
private static final int HASH_INCREMENT = 0x61c88647;

/**
 * Returns the next hash code.
 */
private static int nextHashCode() {
    return nextHashCode.getAndAdd(HASH_INCREMENT);
}

在这里定义了一个类型为AtomicInteger类型的变量,即每次创建一个ThreadLocal对象,都会得到它所对应的值,因为它是自增的,然后threadLocalHashCode变量则为该值得一个hash值。而table变量则是声明在ThreadLocalMap中:

/**
 * The table, resized as necessary.
 * table.length MUST always be a power of two.
 */
private Entry[] table;

它的作用为存储值,其中threadLocalHashCode经过处理后便为ThreadLocal所对应的索引。
获取到Entry对象后,我们就可以通过Entry对象获取到所对应的值,返回即可。

若map为空,则调用setInitialValue方法。

/**
  * Variant of set() to establish initialValue. Used instead
  * of set() in case user has overridden the set() method.
  *
  * @return the initial value
  */
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;
}

方法中,获取到起初始值,并将当前线程与value进行绑定,存入map对象中,以便下一次的调用,若map为空,则创建map对象。

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

我们可以看到就是返回一个ThreadLocalMap对象。

  • 接下来我们看到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);
}

这里的思路很简单,就是先获取到map对象,若map不为空,则更新其值,否则创建一个map对象。

  • 最后我们看到remove方法:
public void remove() {
   ThreadLocalMap m = getMap(Thread.currentThread());
   if (m != null)
       m.remove(this);
}

即获取到map对象后,将map中的值移除。

注意:在使用时,若没有重写initialValue方法,那么在调用get方法前,必须调用set方法,否则会报空指针异常。

TheadLocal的应用

通常当我们想要共享一个变量,但该变量又不是线程安全的,并且也不想通过加锁机制来降低并发性,那么就可以用ThreadLocal来维护一个线程一个实例。

  • 应用一:数据库连接
public class ConnectionManager {
    private static ThreadLocal<Connection> connectionHolder = 
            new ThreadLocal<Connection>(){
                @Override
                protected Connection initialValue() {
                    try {
                        return DriverManager.getConnection("localhost","username","password");
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            };
    
    public static Connection getConnection(){
        return connectionHolder.get();
    }
}
Top