JDK1.6 ThreadLocal

Hibernate中典型的ThreadLocal的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private static final ThreadLocal threadSession = new ThreadLocal();  

public static Session getSession() throws InfrastructureException {
Session s = (Session) threadSession.get();
try {
if (s == null) {
s = getSessionFactory().openSession();
threadSession.set(s);
}
} catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
return s;
}

可以看到在getSession()方法中,首先判断当前线程中有没有放进去session,如果还没有,那么通过sessionFactory().openSession()来创建一个session,再将session set到线程中,实际是放到当前线程的ThreadLocalMap这个map中。这时,对于这个session的唯一引用就是当前线程中的那个ThreadLocalMap,而threadSession作为这个值的key,要取得这个session可以通过threadSession.get()来得到,里面执行的操作实际是先取得当前线程中的ThreadLocalMap,然后将threadSession作为key将对应的值取出。这个session相当于线程的私有变量,而不是public的。
ThreadLocal是在每个线程中有一个map,而将ThreadLocal实例作为key,这样每个map中的项数很少,而且当线程销毁时相应的东西也一起销毁了。 这样设计也防止内存泄漏,1个线程被销毁,这个线程内的对象都被销毁。

源码分析


ThreadLocal内部实现一个hash map的内部类。get()和set()内部都是操作TheadLocalMap#entry。

ThreadLocal#Entry

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* The entries in this hash map extend WeakReference, using
* its main ref field as the key (which is always a
* ThreadLocal object). Note that null keys (i.e. entry.get()
* == null) mean that the key is no longer referenced, so the
* entry can be expunged from table. Such entries are referred to
* as "stale entries" in the code that follows.
*/
static class Entry extends WeakReference<ThreadLocal> {
/** The value associated with this ThreadLocal. */
Object value;

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

/**
* The initial capacity -- MUST be a power of two.
*/
private static final int INITIAL_CAPACITY = 16;

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

/**
* The number of entries in the table.
*/
private int size = 0;

/**
* The next size value at which to resize.
*/
private int threshold; // Default to 0

ThreadLocal内部是由entry这个弱引用对象组成,这个entry就是一个hash map,只是是弱引用。Entry的构造函数Entry(ThreadLocal<?> k, Object v),ThreadLocal引用作为key。根据注释key=null,则不是强引用,因为是弱引用,entry can be expunged from table,这个对象从table中删除。

ThreadLocal#nextHashCode

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

获取下1个hashcode的值。

ThreadLocal#HASH_INCREMENT

1
2
3
4
5
6
/**
* 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;

ThreadLocal#threadLocalHashCode

1
2
3
4
5
6
7
8
9
10
11
/**
* ThreadLocals rely on per-thread linear-probe hash maps attached
* to each thread (Thread.threadLocals and
* inheritableThreadLocals). The ThreadLocal objects act as keys,
* searched via threadLocalHashCode. This is a custom hash code
* (useful only within ThreadLocalMaps) that eliminates collisions
* in the common case where consecutively constructed ThreadLocals
* are used by the same threads, while remaining well-behaved in
* less common cases.
*/
private final int threadLocalHashCode = nextHashCode();

由后面代码可以知道threadLocalHashCode是减少ThreadLocalMap#Entry#table散列冲突。

Thread#threadLocals

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

ThreadLocal#get()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 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();
// 1
ThreadLocalMap map = getMap(t);
if (map != null) {
// 2
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}

标注代码分析

  1. 获取当前线程的ThreadLocal#ThreadLocalMap。
  2. threadlocal#ThreadLocalMap的entry对象。

ThreadLocal#getMap()

1
2
3
4
5
6
7
8
9
10
/**
* 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;
}

获取thread t#threadLocal。

ThreadLocal#ThreadLocalMap#getEntry()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Get the entry associated with key. This method
* itself handles only the fast path: a direct hit of existing
* key. It otherwise relays to getEntryAfterMiss. This is
* designed to maximize performance for direct hits, in part
* by making this method readily inlinable.
*
* @param key the thread local object
* @return the entry associated with key, or null if no such
*/
private Entry getEntry(ThreadLocal key) {
// 1
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);
}

标注代码分析

  1. 减少散列冲突。

ThreadLocal#set()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 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);
}

ThreadLocal#createMap()

1
2
3
4
5
6
7
8
9
10
11
/**
* 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#ThreadLocalMap#set()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Set the value associated with key.
*
* @param key the thread local object
* @param value the value to be set
*/
private void set(ThreadLocal key, Object value) {

// We don't use a fast path as with get() because it is at
// least as common to use set() to create new entries as
// it is to replace existing ones, in which case, a fast
// path would fail more often than not.

Entry[] tab = table;
int len = tab.length;
int i = key.threadLocalHashCode & (len-1);

for (Entry e = tab[i];
e != null;
e = tab[i = nextIndex(i, len)]) {
ThreadLocal k = e.get();
// 1
if (k == key) {
e.value = value;
return;
}
// 2
if (k == null) {
replaceStaleEntry(key, value, i);
return;
}
}
// 3
tab[i] = new Entry(key, value);
int sz = ++size;
if (!cleanSomeSlots(i, sz) && sz >= threshold)
rehash();
}

标注代码分析

  1. 如果threadLocal#key和entry#k相同,则覆盖原来的value。
  2. 如果k=null(entry的key是null),覆盖原来value的值,i是table的下标。
  3. entry的table容量扩展。

ThreadLocal#ThreadLocalMap#replaceStaleEntry()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* Replace a stale entry encountered during a set operation
* with an entry for the specified key. The value passed in
* the value parameter is stored in the entry, whether or not
* an entry already exists for the specified key.
*
* As a side effect, this method expunges all stale entries in the
* "run" containing the stale entry. (A run is a sequence of entries
* between two null slots.)
*
* @param key the key
* @param value the value to be associated with key
* @param staleSlot index of the first stale entry encountered while
* searching for key.
*/
private void replaceStaleEntry(ThreadLocal key, Object value,
int staleSlot) {
Entry[] tab = table;
int len = tab.length;
Entry e;

// Back up to check for prior stale entry in current run.
// We clean out whole runs at a time to avoid continual
// incremental rehashing due to garbage collector freeing
// up refs in bunches (i.e., whenever the collector runs).
// 1
int slotToExpunge = staleSlot;
// 2
for (int i = prevIndex(staleSlot, len);
(e = tab[i]) != null;
i = prevIndex(i, len))

if (e.get() == null)
slotToExpunge = i;

// Find either the key or trailing null slot of run, whichever
// occurs first
for (int i = nextIndex(staleSlot, len);
(e = tab[i]) != null;
i = nextIndex(i, len)) {
ThreadLocal k = e.get();

// If we find key, then we need to swap it
// with the stale entry to maintain hash table order.
// The newly stale slot, or any other stale slot
// encountered above it, can then be sent to expungeStaleEntry
// to remove or rehash all of the other entries in run.
// 3
if (k == key) {
e.value = value;
// 4
tab[i] = tab[staleSlot];
tab[staleSlot] = e;

// Start expunge at preceding stale entry if it exists
// 5
if (slotToExpunge == staleSlot)
slotToExpunge = i;
// 6
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
return;
}

// If we didn't find stale entry on backward scan, the
// first stale entry seen while scanning for key is the
// first still present in the run.
if (k == null && slotToExpunge == staleSlot)
slotToExpunge = i;
}

// If key not found, put new entry in stale slot
// 7
tab[staleSlot].value = null;
tab[staleSlot] = new Entry(key, value);

// If there are any other stale entries in run, expunge them
if (slotToExpunge != staleSlot)
cleanSomeSlots(expungeStaleEntry(slotToExpunge), len);
}

标注代码分析

  1. 待删除的下标。
  2. 往前轮询,如果entry#e#key == null,标记slotToExpunge = 当前i。
  3. key相同时,value替换旧的value值。
  4. 替换tab的数据,即entry。
  5. 如果旧的slot存在,待删除的slot = i。
  6. 清楚相同的slot。
  7. 如果key不存在,创建新的entry。

ThreadLocal#ThreadLocalMap#expungeStaleEntry()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Expunge a stale entry by rehashing any possibly colliding entries
* lying between staleSlot and the next null slot. This also expunges
* any other stale entries encountered before the trailing null. See
* Knuth, Section 6.4
*
* @param staleSlot index of slot known to have null key
* @return the index of the next null slot after staleSlot
* (all between staleSlot and this slot will have been checked
* for expunging).
*/
private int expungeStaleEntry(int staleSlot) {
Entry[] tab = table;
int len = tab.length;

// expunge entry at staleSlot
tab[staleSlot].value = null;
tab[staleSlot] = null;
size--;

// Rehash until we encounter null
// 1
Entry e;
int i;
for (i = nextIndex(staleSlot, len);
(e = tab[i]) != null;
i = nextIndex(i, len)) {
ThreadLocal k = e.get();
// 2
if (k == null) {
e.value = null;
tab[i] = null;
size--;
} else {
// 3
int h = k.threadLocalHashCode & (len - 1);
// 4
if (h != i) {
tab[i] = null;

// Unlike Knuth 6.4 Algorithm R, we must scan until
// null because multiple entries could have been stale.
while (tab[h] != null)
h = nextIndex(h, len);
tab[h] = e;
}
}
}
return i;
}

标注代码分析

  1. ThreadLocal = null,size-1,否则重新计算tab的值。
  2. ThreadLocal是null,删除1个entry table。
  3. h = k#threadlocal#entry的table下标。
  4. h!=i,tab[i]、tab[h]都是null,tab[i]原来值e赋值给tab[h]。
  5. return i表示旧的slot的下一个是null下标。

ThreadLocal#ThreadLocalMap#cleanSomeSlots()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Heuristically scan some cells looking for stale entries.
* This is invoked when either a new element is added, or
* another stale one has been expunged. It performs a
* logarithmic number of scans, as a balance between no
* scanning (fast but retains garbage) and a number of scans
* proportional to number of elements, that would find all
* garbage but would cause some insertions to take O(n) time.
*
* @param i a position known NOT to hold a stale entry. The
* scan starts at the element after i.
*
* @param n scan control: <tt>log2(n)</tt> cells are scanned,
* unless a stale entry is found, in which case
* <tt>log2(table.length)-1</tt> additional cells are scanned.
* When called from insertions, this parameter is the number
* of elements, but when from replaceStaleEntry, it is the
* table length. (Note: all this could be changed to be either
* more or less aggressive by weighting n instead of just
* using straight log n. But this version is simple, fast, and
* seems to work well.)
*
* @return true if any stale entries have been removed.
*/
private boolean cleanSomeSlots(int i, int n) {
boolean removed = false;
Entry[] tab = table;
int len = tab.length;
do {
i = nextIndex(i, len);
Entry e = tab[i];
// 1
if (e != null && e.get() == null) {
n = len;
removed = true;
i = expungeStaleEntry(i);
}
} while ( (n >>>= 1) != 0);
return removed;
}

标注代码分析

  1. 清除entry!=null,但是entry#value = null的值。

程序实例

证明每个线程在ThreadLocal都是私有的。set单值和多值,tab的下标变化。

同一个线程多个值

tab下标的变化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package threadLocal;

public class T {

protected static ThreadLocal<String> threadLocal = new ThreadLocal<String>();

public static void main(String[] args) {
System.out.println("threadLocal = " + threadLocal);
threadLocal.set("1000");

System.out.println("main = " + threadLocal.get());
threadLocal.set("4000");
System.out.println("main = " + threadLocal.get());
}

}



threadLocalMap = 64a294a6
threadLocal = 6f2b958e

value=1000,i=3,存在tab[3]

新建一个Entry对象,第1次e对象是null。


第2次set值,value=4000,threadLocalMap和threadLocal是同一个。


多个线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package threadLocal;

public class T {

public static ThreadLocal<String> threadLocal = new ThreadLocal<String>();

public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("执行线程1.....");
threadLocal.set("1001");

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("t1 = " + T.threadLocal.get());
}
});
System.out.println("thread1 = " + t1);
t1.start();

Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("执行线程2.....");
threadLocal.set("1002");

try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("t2 = " + T.threadLocal.get());
}
});
System.out.println("thread2 = " + t2);
t2.start();

}

}



T1线程没有threadLocal,需要新建。

总结

  • ThreadLocal的get,set方法,key值都是ThreadLocal本身,但是线程(Thread)对ThreadLocal是私有的,每个线程都有自己的ThreadLocal。
  • 线程销毁的时候,ThreadLocal也跟着销毁,这样不会造成内存溢出。
  • 根据实例2,多线程使用ThreadLocal互不影响。因为,ThreadLocal里有个内部类ThreadLocalMap,每个线程都私有一个ThreadLocalMap,ThreadLocalMap里有个Entry的hash map结构的类,通过table[]来存取ThreadLocal,每个线程的ThreadLocalMap相互不影响。

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×