`

ThreadLocal源码分析

阅读更多

在阅读《Java Concurrency In Practice》时,书中提到ThreadLocal是一种更为规范常用的Thread Confine方式。于是想仔细分析一下ThreadLocal的实现方式。曾经转载了一篇关于ThreadLocal的文章:hi.baidu.com/gefforey520/blog/item/c3bb64fa4ad1779358ee902c.html,其中提到ThreadLocal的实现方式是声明一个Hashtable,然后以Thread.currentThread()为key,变量的拷贝为value。今天阅读源码才知道实现方式已经大为改变,下面来看代码。
/**
* 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();

/**
* 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);
}
/**
* Creates a thread local variable.
*/
public ThreadLocal() {
}
ThreadLocal只有三个变量,从构造函数知道,在创建一个ThreadLocal实例时,只是调用nextHashCode方法将nextHashCode的值赋给实例的threadLocalHashCode,然后nextHashCode的值增加HASH_INCREMENT这个值。 因此ThreadLocal实例的变量只有threadLocalHashCode,而且是final的,用来区分不同的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();
}
其中调用getMap(Thread t)返回ThreadLocalMap,ThreadLocalMap是内部静态类,部分代码如下:
/**
* ThreadLocalMap is a customized hash map suitable only for maintaining
* thread local values. No operations are exported outside of the
* ThreadLocal class. The class is package private to allow declaration of
* fields in class Thread. To help deal with very large and long-lived
* usages, the hash table entries use WeakReferences for keys. However,
* since reference queues are not used, stale entries are guaranteed to be
* removed only when the table starts running out of space.
*/
static class ThreadLocalMap {

/**
* 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
Entry继承WeakReference,通过其注释并结合WeakReference的功能,我们知道:一旦没有指向 key 的强引用, ThreadLocalMap 在 GC 后将自动删除相关的 entry。ThreadLocalMap采用数组来保存Entry,并且Entry中以ThreadLocal为key,初始大小为16.
        接着看ThreadLocalMap的constructor:
/**
* Construct a new map initially containing (firstKey, firstValue).
* ThreadLocalMaps are constructed lazily, so we only create one when we
* have at least one entry to put in it.
*/
ThreadLocalMap(ThreadLocal firstKey, Object firstValue) {
table = new Entry[INITIAL_CAPACITY];
int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);
table[i] = new Entry(firstKey, firstValue);
size = 1;
setThreshold(INITIAL_CAPACITY);
}
/**
* Construct a new map including all Inheritable ThreadLocals from given
* parent map. Called only by createInheritedMap.
*
* @param parentMap
*            the map associated with parent thread.
*/
private ThreadLocalMap(ThreadLocalMap parentMap) {
Entry[] parentTable = parentMap.table;
int len = parentTable.length;
setThreshold(len);
table = new Entry[len];

for (int j = 0; j < len; j++) {
Entry e = parentTable[j];
if (e != null) {
ThreadLocal key = e.get();
if (key != null) {
Object value = key.childValue(e.value);
Entry c = new Entry(key, value);
int h = key.threadLocalHashCode & (len - 1);
while (table[h] != null)
h = nextIndex(h, len);
table[h] = c;
size++;
}
}
}
}
ThreadLocalMap有两个构造函数,可以直接传入ThreadLcoal-value对,也可以传入一个ThreadLocalMap,传入ThreadLocalMap的时候,会依次将其Entry存放在table中。接着来分析get方法:
/**
* 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) {
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);
}
通过实例变量threadLocalHashCode算出下标,然后返回其值。set和remove方法类似。
继续看ThreadLocal类的get方法,通过getMap(Thread t)返回ThreadLocalMap,然后从ThreadLocalMap中通过getEntry(ThreadLocal key) 取出值。下面继续看getMap(Thread t)方法:
/**
* 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类也持有ThreadLocalMap,这样每个线程的变量都存放在自己的ThreadLocalMap中,可谓名符其实。
继续看Thread类如何操作ThreadLocalMap:
/*
* ThreadLocal values pertaining to this thread. This map is maintained by
* the ThreadLocal class.
*/
ThreadLocal.ThreadLocalMap threadLocals = null;

/*
* InheritableThreadLocal values pertaining to this thread. This map is
* maintained by the InheritableThreadLocal class.
*/
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
Thread类中声明了两个ThreadLocalMap变量,
/**
* Initializes a Thread.
*
* @param g
*            the Thread group
* @param target
*            the object whose run() method gets called
* @param name
*            the name of the new Thread
* @param stackSize
*            the desired stack size for the new thread, or zero to indicate
*            that this parameter is to be ignored.
*/
private void init(ThreadGroup g, Runnable target, String name, long stackSize) {
Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
/* Determine if it's an applet or not */

/*
* If there is a security manager, ask the security manager what to
* do.
*/
if (security != null) {
g = security.getThreadGroup();
}

/*
* If the security doesn't have a strong opinion of the matter use
* the parent thread group.
*/
if (g == null) {
g = parent.getThreadGroup();
}
}

/*
* checkAccess regardless of whether or not threadgroup is explicitly
* passed in.
*/
g.checkAccess();

/*
* Do we have the required permissions?
*/
if (security != null) {
if (isCCLOverridden(getClass())) {
security.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
}
}

g.addUnstarted();

this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
this.name = name.toCharArray();
if (security == null || isCCLOverridden(parent.getClass()))
this.contextClassLoader = parent.getContextClassLoader();
else
this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext = AccessController.getContext();
this.target = target;
setPriority(priority);
if (parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
/* Stash the specified stack size in case the VM cares */
this.stackSize = stackSize;

/* Set thread ID */
tid = nextThreadID();
}
在init方法中,存在着对inheritableThreadLocals的操作:
if (parent.inheritableThreadLocals != null)
this.inheritableThreadLocals =ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);
而ThreadLocal的createInheritedMap方法则是调用ThreadLocalMap类传入ThreadLocalMap参数的构造函数。
也就是说在Thread类中,当前线程会调用init方法去初始一个线程,而在init方法中,会将当前线程的inheritableThreadLocals拷贝给等待初始化的线程。这让我联想起unix/linux系统中,父线程会调用fork()函数生成一个子线程,而且会把父线程大部分的信息拷贝给子线程。
         最后来看Thread类的exit方法:
/**
* This method is called by the system to give a Thread a chance to clean up
* before it actually exits.
*/
private void exit() {
if (group != null) {
group.remove(this);
group = null;
}
/* Aggressively null out all reference fields: see bug 4006245 */
target = null;
/* Speed the release of some of these resources */
threadLocals = null;
inheritableThreadLocals = null;
inheritedAccessControlContext = null;
blocker = null;
uncaughtExceptionHandler = null;
}
在线程真正终止前会执行这个方法,这个方法会把threadLocals和inheritableThreadLocals指向null。但我在Thread类中并没有看到对threadLocals的赋值,应该是通过ThreadLocal来设置的。
        写了个简单的Thread测试程序,只是想跟踪一下上述两个ThreadLocalMap变量的状态:
public class TimePrinter extends Thread {

public void run() {
while (true) {
try {
System.out.println(new Date(System.currentTimeMillis()));
} catch (Exception e) {
System.out.println(e);
}
}
}

static public void main(String args[]) {
TimePrinter tp1 = new TimePrinter();
tp1.start();
ThreadLocal t2 = new ThreadLocal();
t2.set("aaaaaaaaaaaaaaaaaaaaaaaa");
}
}
可以看到,启动一个线程,不停打印系统时间,然后通过ThreadLocal给当前线程添加一份字符串,观察有:




inheritableThreadLocals中有一个Entry,但value为null,threadLocals中有三个Entry,其中两个value不明,一个为ThreadLocal设置的值。不过我实在不知道其他三个Entry值是如何设置的,留个疑问。
总结:ThreadLocal实例只有一个threadLocalHashCode值,ThreadLocal给各个线程设置的值都是存在各个线程threadLocals里 。相比Hashtable的实现方式,现在的方式更为合理。当一个线程终止时,其inheritableThreadLocals和threadLocals均被置为null,于是通过TreadLocal也就无法访问这个线程;而当ThreadLocal被设置为null时,Thread里threadLocals就会移除key为ThreadLocal的Entry。Hashtable的实现方式则无法实现这一点。最为关键的是Hashtable的实现需要同步,所带来的性能损耗是很大的,而现在的方式则不需要同步。性能提升很大。

转自http://hi.baidu.com/gefforey520/blog/item/ee476de928908735b80e2d97.html

分享到:
评论

相关推荐

    ThreadLocal源码分析和使用

    ThreadLocal 源码分析和使用 ThreadLocal 是 Java 语言中的一种多线程编程机制,用于解决多线程程序的并发问题。它不是一个 Thread,而是一个 Thread 的局部变量。ThreadLocal 的出现是为了解决多线程程序中的共享...

    ThreadLocal_ThreadLocal源码分析_

    **ThreadLocal概述** ThreadLocal是Java中的一个线程局部变量类,它为每个线程创建了一个独立的变量副本。这意味着每个线程都有自己的ThreadLocal变量,互不干扰,提供了线程安全的数据存储方式。ThreadLocal通常...

    ThreadLocal_ThreadLocal源码分析_源码.zip

    同时,`ThreadLocalMap`的构造、扩容、收缩以及弱引用的处理等都是源码分析的重点。 总的来说,ThreadLocal是一个强大的工具,可以有效解决多线程环境中的数据隔离问题,但在使用过程中需要注意内存管理,防止潜在...

    Java并发编程学习之ThreadLocal源码详析

    Java并发编程学习之ThreadLocal源码详析 ThreadLocal是Java并发编程中的一种机制,用于解决多线程访问共享变量的问题。它可以使每个线程对共享变量的访问都是线程安全的,使得多线程编程变得更加简单。 ...

    java并发包源码分析(3)ThreadLocal

    Java并发编程中,ThreadLocal是一个非常重要的类,它是解决线程安全问题的一个工具。ThreadLocal提供了一种在多线程环境下使用“共享变量”的方式,但是它实际上是为每个线程提供一个变量的副本,这样每个线程都可以...

    ThreadLocal 线程本地变量 及 源码分析.rar_开发_设计

    以上是对ThreadLocal的简单介绍和源码分析,实际使用中还需要结合具体业务场景进行合理设计和优化。通过深入理解ThreadLocal的工作原理,可以帮助我们更好地利用这一工具,提高代码的并发性能和可维护性。

    Java源码解析ThreadLocal及使用场景

    ThreadLocal的源码分析: 首先是类的介绍。ThreadLocal类提供了线程本地变量。这些变量使每个线程都有自己的一份拷贝。ThreadLocal期望能够管理一个线程的状态,例如用户id或事务id。 ThreadLocal的使用有很多...

    ThreadLocal的原理,源码深度分析及使用.docx

    ThreadLocal 的原理、源码深度分析及使用 ThreadLocal 是 Java 语言中的一种机制,用于实现线程本地存储,能够帮助开发者在多线程环境下解决变量访问安全的问题。下面将对 ThreadLocal 的原理、实现机制、使用方法...

    druid 源码分析 逐层详解

    标题所指的知识点为“Druid 源码分析 逐层详解”,意味着我们需要深入分析Druid这一开源数据处理工具的源码,并从不同的层面揭示其内部实现机制。 首先,我们来看Druid的构架设计。Druid采用了分层的架构,每个层次...

    ThreadLocal分析

    在分析ThreadLocal源码时,可以了解到它如何在内部实现线程隔离,以及ThreadLocalMap的结构和工作方式。理解这些细节有助于我们更好地利用ThreadLocal,同时避免可能出现的问题。通过阅读源码,我们可以发现...

    ThreadLocal内存泄露分析

    【标题】:“ThreadLocal内存泄露分析” 在Java编程中,ThreadLocal是一个强大的工具类,它为每个线程提供了一个独立的变量副本,使得每个线程都可以独立地改变自己的副本,而不会影响其他线程所对应的副本。然而,...

    JUC并发编程与源码分析视频课.zip

    《JUC并发编程与源码分析视频课》是一门深入探讨Java并发编程的课程,主要聚焦于Java Util Concurrency(JUC)库的使用和源码解析。JUC是Java平台提供的一组高级并发工具包,它极大地简化了多线程编程,并提供了更...

    threadLocal

    源码分析: 在Java的`ThreadLocal`实现中,每个线程都有一个`ThreadLocalMap`,它是`ThreadLocal`的一个内部类,用于存储线程局部变量。`ThreadLocalMap`使用弱引用作为键,目的是在线程不再引用ThreadLocal对象时,...

    ThreadLocal的用处

    6. **源码分析**: ThreadLocal类的核心在于`initialValue()`方法和`get()`方法。`initialValue()`是线程首次访问ThreadLocal变量时调用的,返回的是默认值。`get()`方法则会查找当前线程的ThreadLocalMap,如果...

    Java并发编程中ThreadLocal的原理与应用分析

    内容概要:本文深入解析了Java中的ThreadLocal工具类,包括其常见应用场景及其原理和源码分析。通过Demo示例介绍了如何利用ThreadLocal实现线程间的隔离,使各个线程可以拥有独立的变量副本而不互相干扰。文章详细...

    JDK的ThreadLocal理解(一)使用和测试

    **标题:“JDK的ThreadLocal理解(一)使用...通过以上分析,我们可以看到ThreadLocal在实现线程间数据隔离、简化多线程编程方面的作用。然而,使用时也要注意避免内存泄漏和过度依赖,合理规划其在系统架构中的位置。

    ThreadLocal

    5. **源码分析**: - `ThreadLocal`类在`java.lang`包下,它的实现主要依赖于`Thread`类的成员变量`threadLocals`,这是一个`ThreadLocalMap`实例,`ThreadLocalMap`是`WeakReference&lt;ThreadLocal&lt;?&gt;&gt;`和`Object`...

    对ThreadLocal的理解【源码分析+应用举例】

    一、简介 ThreadLocal是JDK包提供的,它提供了线程本地变量,也就是如果你创建了一个ThreadLocal变量,那么访问这个变量的每一个线程都会有这个变量的一个本地副本。...根据源码,画出ThreadLocal原理图 原创文章

    java并发源码分析之实战编程

    "java并发源码分析之实战编程"这个主题深入探讨了Java平台上的并发处理机制,旨在帮助开发者理解并有效地利用这些机制来提高程序性能和可扩展性。在这个专题中,我们将围绕Java并发库、线程管理、锁机制、并发容器...

Global site tag (gtag.js) - Google Analytics