ThreadLocal(一)

2014-11-24 03:19:40 · 作者: · 浏览: 2

ThreadLocal是什么呢?其实ThreadLocal不是一个线程的本地实现版本,也不是一个Thread。

ThreadLocal的目的就是为每一个使用ThreadLocal的线程都提供一个值,

让该值和使用它的线程绑定,当然每一个线程都可以独立地改变它绑定的值。

主要函数

Public Methods

T get()

Returns the value of this variable for the current thread.

返回当前线程的线程局部变量副本

void remove()

Removes the entry for this variable in the current thread.

如果我们想把ThreadLocal所绑定的对象的引用清空,请不要粗暴的把ThreadLocal设置null,而应该调用remove()方法

void set(T value)

Sets the value of this variable for the current thread.

设置当前线程的线程局部变量副本的值

Protected Methods

T initialValue()

Provides the initial value of this variable for the current thread.

该方法返回当前线程在该线程局部变量的初始值。这个方法是一个延迟调用方法,在一个线程第1次调用get()且此时还没调用set(Object)时才执行,并且仅执行1次。ThreadLocal中返回的是null。该方法是一个protected的方法,它主要是为设置局部变量的初始值提供方便。

ThreadLocal是如何做到让每一个线程和一个值绑定的呢?

其实实现的思路很简单,在ThreadLocal类中有一个Map,用于存储每一个线程的变量的副本。

比如下面的示例实现:

示例1:

public class ThreadLocal {

private Map values = Collections.synchronizedMap(new HashMap());

public Object get() {

Thread curThread = Thread.currentThread();

Object o = values.get(curThread);

if (o == null && !values.containsKey(curThread)) {

o = initialValue();

values.put(curThread, o);

}

return o;

}

public void set(Object newValue) {

values.put(Thread.currentThread(), newValue);

}

public Object initialValue() {

return null;

}

}

注意:以上只是一个粗略的实现。我觉得上面的没有必要使用Collections.synchronizedMap。

因为不同的线程不可能对HashMap的同一项进行操作。

在JDK中的ThreadLocal的实现感觉很巧妙:

下面是去掉了注释后ThreadLocal的代码

public class ThreadLocal {

private final int threadLocalHashCode = nextHashCode();

private static AtomicInteger nextHashCode =

new AtomicInteger();

private static final int HASH_INCREMENT = 0x61c88647;

private static int nextHashCode() {

return nextHashCode.getAndAdd(HASH_INCREMENT);

}

protected T initialValue() {

return null;

}

public ThreadLocal() {

}

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();

}

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;

}

public void set(T value) {

Thread t = Thread.currentThread();

ThreadLocalMap map = getMap(t);

if (map != null)

map.set(this, value);

else

createMap(t, value);

}

public void remove() {

ThreadLocalMap m = getMap(Thread.currentThread());

if (m != null)

m.remove(this);

}

ThreadLocalMap getMap(Thread t) {

return t.threadLocals;

}

void createMap(Thread t, T firstValue) {

t.threadLocals = new ThreadLocalMap(this, firstValue);

}

static ThreadLocalMap createInheritedMap(ThreadLocalMap parentMap) {

return new ThreadLocalMap(parentMap);

}

T childValue(T parentValue) {

throw new UnsupportedOperationException();

}

注意1:每个线程有个ThreadLocalMap threa