ThreadLocal类源码
来源:互联网 发布:计算机专业与大数据 编辑:程序博客网 时间:2024/06/11 00:05
下面来看看ThreadLocal的实现原理(jdk1.5源码)
代码
- public class ThreadLocal<T> {
- /**
- * ThreadLocals rely on per-thread 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. Accessed only by like-named method.
- */
- private static int nextHashCode = 0;
- /**
- * 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;
- /**
- * Compute the next hash code. The static synchronization used here
- * should not be a performance bottleneck. When ThreadLocals are
- * generated in different threads at a fast enough rate to regularly
- * contend on this lock, memory contention is by far a more serious
- * problem than lock contention.
- */
- private static synchronized int nextHashCode() {
- int h = nextHashCode;
- nextHashCode = h + HASH_INCREMENT;
- return h;
- }
- /**
- * Creates a thread local variable.
- */
- public ThreadLocal() {
- }
- /**
- * Returns the value in the current thread's copy of this thread-local
- * variable. Creates and initializes the copy if this is the first time
- * the thread has called this 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)
- return (T)map.get(this);
- // Maps are constructed lazily. if the map for this thread
- // doesn't exist, create it, with this ThreadLocal and its
- // initial value as its only entry.
- T value = initialValue();
- createMap(t, value);
- return value;
- }
- /**
- * Sets the current thread's copy of this thread-local variable
- * to the specified value. Many applications will have no need for
- * this functionality, relying solely on the {@link #initialValue}
- * method to set the values of thread-locals.
- *
- * @param value the value to be stored in the current threads' 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 the map associated with a ThreadLocal. Overridden in
- * InheritableThreadLocal.
- *
- * @param t the current thread
- * @return the map
- */
- ThreadLocalMap getMap(Thread t) {
- return t.threadLocals;
- }
- /**
- * 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);
- }
- .......
- /**
- * 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 {
- ........
- }
- }
0 0
- 源码浅析ThreadLocal类
- ThreadLocal类源码
- ThreadLocal类源码解析
- 【ThreadLocal】深入JDK源码之ThreadLocal类
- 【ThreadLocal】深入JDK源码之ThreadLocal类
- Java8 ThreadLocal类源码 详解
- ThreadLocal源码
- JDK源码解析之ThreadLocal类
- JDK源码解析之ThreadLocal类
- ThreadLocal类详解与源码分析
- ThreadLocal类的用法与源码解析
- 深入JDK源码之ThreadLocal类
- ThreadLocal源码分析
- Threadlocal源码分析
- ThreadLocal源码解析
- ThreadLocal源码分析
- ThreadLocal源码分析
- ThreadLocal源码分析
- july算法课笔记
- 项目1-三角行雏形设计三角行带参的构造函数
- C++学习笔记55——类模板的输入输出操作符
- Javascript中apply、call、bind
- 深入理解Tomcat系列之七:详解URL请求
- ThreadLocal类源码
- 何为大,世间没有比生命更大,珍惜生命
- c#+windows环境 编写mysql集群服务
- 1个viewController怎么关联2个XIB,一个是iphone的XIB,一个是ipad的xib
- JS学后感受
- HDU 3361 ASCII
- java图形界面异常处理的简单利用
- hdoj--1789--Doing Homework again(贪心模拟)
- 二叉树前、中、后序遍历(递归与非递归)