当前位置: 首页 > 编程日记 > 正文

正确理解ThreadLocal

详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt107

首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中的对象是该线程自己使用的对象,其他线程是不需要访问的,也访问不到的。各个线程中访问的是不同的对象。 

另外,说ThreadLocal使得各线程能够保持各自独立的一个对象,并不是通过ThreadLocal.set()来实现的,而是通过每个线程中的new 对象 的操作来创建的对象,每个线程创建一个,不是什么对象的拷贝或副本。通过ThreadLocal.set()将这个新创建的对象的引用保存到各线程的自己的一个map中,每个线程都有这样一个map,执行ThreadLocal.get()时,各线程从自己的map中取出放进去的对象,因此取出来的是各自自己线程中的对象,ThreadLocal实例是作为map的key来使用的。 

如果ThreadLocal.set()进去的东西本来就是多个线程共享的同一个对象,那么多个线程的ThreadLocal.get()取得的还是这个共享对象本身,还是有并发访问问题。 

下面来看一个hibernate中典型的ThreadLocal的应用:

  1. 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的。 
显然,其他线程中是取不到这个session的,他们也只能取到自己的ThreadLocalMap中的东西。要是session是多个线程共享使用的,那还不乱套了。 
试想如果不用ThreadLocal怎么来实现呢?可能就要在action中创建session,然后把session一个个传到service和dao中,这可够麻烦的。或者可以自己定义一个静态的map,将当前thread作为key,创建的session作为值,put到map中,应该也行,这也是一般人的想法,但事实上,ThreadLocal的实现刚好相反,它是在每个线程中有一个map,而将ThreadLocal实例作为key,这样每个map中的项数很少,而且当线程销毁时相应的东西也一起销毁了,不知道除了这些还有什么其他的好处。 

总之,ThreadLocal不是用来解决对象共享访问问题的,而主要是提供了保持对象的方法和避免参数传递的方便的对象访问方式。归纳了两点: 
1。每个线程中都有一个自己的ThreadLocalMap类对象,可以将线程自己的对象保持到其中,各管各的,线程可以正确的访问到自己的对象。 
2。将一个共用的ThreadLocal静态实例作为key,将不同对象的引用保存到不同线程的ThreadLocalMap中,然后在线程执行的各处通过这个静态ThreadLocal实例的get()方法取得自己线程保存的那个对象,避免了将这个对象作为参数传递的麻烦。 

当然如果要把本来线程共享的对象通过ThreadLocal.set()放到线程中也可以,可以实现避免参数传递的访问方式,但是要注意get()到的是那同一个共享对象,并发访问问题要靠其他手段来解决。但一般来说线程共享的对象通过设置为某类的静态变量就可以实现方便的访问了,似乎没必要放到线程中。 

ThreadLocal的应用场合,我觉得最适合的是按线程多实例(每个线程对应一个实例)的对象的访问,并且这个对象很多地方都要用到。 

下面来看看ThreadLocal的实现原理(jdk1.5源码)

  1. 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
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    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 {  
      
        ........  
      
        }  
      
    }



可以看到ThreadLocal类中的变量只有这3个int型:

Java代码  收藏代码

  1. 1
    2
    3
    private final int threadLocalHashCode = nextHashCode();  
    private static int nextHashCode = 0;  
    private static final int HASH_INCREMENT = 0x61c88647;


而作为ThreadLocal实例的变量只有 threadLocalHashCode 这一个,nextHashCode 和HASH_INCREMENT 是ThreadLocal类的静态变量,实际上HASH_INCREMENT是一个常量,表示了连续分配的两个ThreadLocal实例的threadLocalHashCode值的增量,而nextHashCode 的表示了即将分配的下一个ThreadLocal实例的threadLocalHashCode 的值。 

可以来看一下创建一个ThreadLocal实例即new ThreadLocal()时做了哪些操作,从上面看到构造函数ThreadLocal()里什么操作都没有,唯一的操作是这句:

Java代码  收藏代码

  1. 1
    private final int threadLocalHashCode = nextHashCode();


那么nextHashCode()做了什么呢:

Java代码  收藏代码

  1. 1
    2
    3
    4
    5
    private static synchronized int nextHashCode() {  
        int h = nextHashCode;  
        nextHashCode = h + HASH_INCREMENT;  
        return h;  
    }

就是将ThreadLocal类的下一个hashCode值即nextHashCode的值赋给实例的threadLocalHashCode,然后nextHashCode的值增加HASH_INCREMENT这个值。 

因此ThreadLocal实例的变量只有这个threadLocalHashCode,而且是final的,用来区分不同的ThreadLocal实例,ThreadLocal类主要是作为工具类来使用,那么ThreadLocal.set()进去的对象是放在哪儿的呢? 

看一下上面的set()方法,两句合并一下成为

Java代码  收藏代码

  1. 1
    ThreadLocalMap map = Thread.currentThread().threadLocals;


这个ThreadLocalMap 类是ThreadLocal中定义的内部类,但是它的实例却用在Thread类中:

Java代码  收藏代码

  1. 1
    2
    3
    4
    5
    6
    7
    8
    public class Thread implements Runnable {  
        ......  
      
        /* ThreadLocal values pertaining to this thread. This map is maintained 
         * by the ThreadLocal class. */
        ThreadLocal.ThreadLocalMap threadLocals = null;    
        ......  
    }



再看这句:

Java代码  收藏代码

  1. 1
    2
    if (map != null)  
        map.set(this, value);


也就是将该ThreadLocal实例作为key,要保持的对象作为值,设置到当前线程的ThreadLocalMap 中,get()方法同样大家看了代码也就明白了,ThreadLocalMap 类的代码太多了,我就不帖了,自己去看源码吧。 

写了这么多,也不知讲明白了没有,有什么不当的地方还请大家指出来。

转载于:https://www.cnblogs.com/grefr/p/6095064.html

相关文章:

PHP-密码学算法及其应用-对称密码算法

转自&#xff1a;http://www.smatrix.org/bbs/simple/index.php?t5662.html //目录1. PHP的散列函数及其应用2. PHP中的对称密码算法及其应用3. PHP的公钥密码算法及其应用///2 PHP中的对称密码算法及其应用前一段时间一直想写完PHP中的密码学算法及其应用的三大部分…

Swift4 String截取字符串

var str1 "AlexanderYeah";// 1 截取字符串的第一种方式 // prefix 截取前3个字符串 var str2 str1.prefix(3); print(str2);// suffix 截取后3个字符串 var str3 str1.suffix(3); print(str3);// 2 截取一个范围的字符串 // 从0开始 到倒数第二位结束 let idx1 …

angular react_Angular 2 vs React:将会有鲜血

angular reactAngular 2 has reached Beta and appears poised to become the hot new framework of 2016. It’s time for a showdown. Let’s see how it stacks up against 2015’s darling: React.Angular 2已达到Beta版本&#xff0c;并有望成为2016年炙手可热的新框架。该…

Welcome to Swift (苹果官方Swift文档初译与注解三十四)---241~247页(第五章-- 函数)

In-Out Parameters (全局参数) 像前面描述的参数变量,只能在函数体内进行修改,如果你需要函数修改的它的参数值,并且希望这些改变在函数调用结束后仍然有效,可以定义使用全局参数. 定义全局参数使用关键字inout,全局参数的值在函数调用的时候进行传递,在函数体内进行修改,最后函…

递归 尾递归_代码简报:递归,递归,递归

递归 尾递归Here are three stories we published this week that are worth your time:这是我们本周发布的三个值得您关注的故事&#xff1a; A beginner’s guide to recursion: 6 minute read 递归初学者指南&#xff1a; 6分钟阅读 Things you probably didn’t know you …

Hadoop 生态系统

当下 Hadoop 已经成长为一个庞大的生态体系&#xff0c;只要和海量数据相关的领域&#xff0c;都有 Hadoop 的身影。下图是一个 Hadoop 生态系统的图谱&#xff0c;详细列举了在 Hadoop 这个生态系统中出现的各种数据工具。这一切&#xff0c;都起源自 Web 数据爆炸时代的来临。…

socket通信——通过Udp传输方式,将一段文字数据发送出去

需求&#xff1a;通过Udp传输方式&#xff0c;将一段文字数据发送出去定义一个Udp发送端思路&#xff1a;1、建立updsocket服务2、提供数据&#xff0c;并将数据封装到数据包中。3、通过socket服务的发送功能&#xff0c;将数据包发出去4、关闭资源。import java.net.*; class …

编码中统一更该变量的快捷键_流媒体的7种方式使您成为更好的编码器

编码中统一更该变量的快捷键by freeCodeCamp通过freeCodeCamp 流媒体的7种方式使您成为更好的编码器 (7 Ways Streaming Makes you a Better Coder) After coding live on twitch.tv for dozens of hours, I’m convinced that streaming makes you a better coder. Here’s w…

AutoConfig工具使用

下载安装Auto工具包&#xff1a; http://code.taobao.org/mvn/repository/com/alibaba/citrus/tool/antx-autoconfig/1.0.9/antx-autoconfig-1.0.9.tgzhttp://code.taobao.org/mvn/repository/com/alibaba/citrus/tool/antx-autoexpand/1.0.9/antx-autoexpand-1.0.9.tgztar zxv…

Spark2 ML 学习札记

摘要&#xff1a;  1.pipeline 模式 1.1相关概念 1.2代码示例  2.特征提取&#xff0c;转换以及特征选择 2.1特征提取 2.2特征转换 2.3特征选择 3.模型选择与参数选择 3.1 交叉验证 3.2 训练集-测试集 切分 4.spark新增SparkSession与DataSet 内容&#xff1a; 1.pipeline …

xCode 开发快捷键

Ctrl CMD 右箭头返回上一个编辑的界面Ctrl CMD 左箭头返回后一个编辑的界面CMD Option 左箭头区域代码折叠CMD Option 右箭头区域代码展开Shift CMD Option 左箭头折叠界面内所有的代码Shift CMD Option 右箭头展开界面内所有的代码CMD Ctrl 上下箭头.h 和 .m …

javascript模块_JavaScript模块第2部分:模块捆绑

javascript模块by Preethi Kasireddy通过Preethi Kasireddy JavaScript模块第2部分&#xff1a;模块捆绑 (JavaScript Modules Part 2: Module Bundling) In Part I of this post, I talked about what modules are, why developers use them, and the various ways to incorp…

idea上实现github代码同步

1.先将github远程仓库clone到本地 2.将本地仓库中的项目导入到idea中 3.如果你的项目代码不是放在仓库的根目录下&#xff0c;idea会识别到你的项目是在git仓库目录下&#xff0c;必须点击add root才能匹配路径。 4.add root后会发现右击项目时会多了一个git选项 5.在git选项中…

iOS12 UITabbar Item 向上漂移错位的bug

[[UITabBar appearance] setTranslucent:NO]; 加此行代码 完美解决此bug

jQuery学习笔记(一)

补充一些自己容易忘的知识点: event.stopPropagation() 阻止事件冒泡 event.preventDefault() 阻止事件的默认行为 return false 相当于event.stopPropagation() event.preventDefault() 。除了阻止默认行为之外&#xff0c;还会阻止事件冒泡。 转载于:https://www.cnblogs.…

随机网络构建_构建随机报价机

随机网络构建by Ayo Isaiah通过Ayo Isaiah 构建随机报价机 (Building a Random Quote Machine) I really wasn’t entirely satisfied with my first attempt at building a Random Quote Generator on Free Code Camp. It was ugly, and the quotes were too long, so I didn…

20145231 《信息安全系统设计基础》第11周学习总结

20145231《信息安全系统设计基础》第11周学习总结 教材学习内容总结 异常 异常是异常控制流的一种形式&#xff0c;由硬件和操作系统实现。简单来说&#xff0c;就是控制流中的突变。 出现异常的处理方式&#xff1a; 1.处理器检测到有异常发生 2.通过异常表&#xff0c;进行间…

JAR命令使用

jar 命令详解 jar 是随 JDK 安装的&#xff0c;在 JDK 安装目录下的 bin 目录中&#xff0c;Windows 下文件名为 jar.exe&#xff0c;Linux 下文件名为 jar。它的运行需要用到 JDK 安装目录下 lib 目录中的 tools.jar 文件。不过我们除了安装 JDK 什么也不需要做&#xff0c;因…

捍卫者usb管理控制系统_捍卫超模块化JavaScript

捍卫者usb管理控制系统by Mike Groseclose通过Mike Groseclose 捍卫超模块化JavaScript (In Defense of Hyper Modular JavaScript) Last week npmgate was a big topic for the JavaScript community. For those of you who haven’t been following what happened, here’s …

Android开发——布局性能优化的一些技巧(一)

0. 前言上一篇我们分析了为什么LinearLayout会比RelativeLayout性能更高&#xff0c;意义在于分析了这两种布局的实现源码&#xff0c;算是对一个小结论的证明过程&#xff0c;但是对布局性能的优化效果&#xff0c;对这两种布局的选择远不如减少布局层级、避免过分绘制、按需加…

1-RAC基础

1 安装 pod ‘ReactiveObjC’ RAC 其实大大减少了代码量 2 基本使用 // 0 RAC 中最为常见的类 信号类/*RACSignal&#xff1a;信号类1.通过RACSignal 创建1个信号&#xff08;默认&#xff1a;冷信号&#xff09;2.通过订阅者&#xff0c;订阅信号信号&#xff08;变成:热信号…

static用法总结

C的static有两种用法&#xff1a;面向过程程序设计中的static和面向对象程序设计中的static。前者应用于普通变量和函数&#xff0c;不涉及类&#xff1b;后者主要说明static在类中的作用。 一、面向过程设计中的static1、静态全局变量2、静态局部变量3、静态函数二、面向对象的…

小程序 缩放_缩放流星应用程序的初体验

小程序 缩放by Elie Steinbock埃莉斯坦博克(Elie Steinbock) 缩放流星应用程序的初体验 (First Experiences Scaling a Meteor App) I recently went through the challenge and ordeal of having to scale my Meteor app. It’s a project that had already been running in …

SQL Server Lock Escalation - 锁升级

Articles Locking in Microsoft SQL Server (Part 12 – Lock Escalation) http://dba.stackexchange.com/questions/12864/what-is-lock-escalation 2008 R2 Lock Escalation (Database Engine)---Forward from Locking in Microsoft SQL Server (Part 12 – Lock Escalation)…

Jzzhu and Chocolate

CF#257 div2 C:http://codeforces.com/contest/450/problem/C 题意&#xff1a;n*m的方格&#xff0c;每次可以横着或者纵向的切一刀&#xff0c;问切k之后&#xff0c;最小的最大是多少。 题解&#xff1a;比赛的时候没有想到怎么处理&#xff0c;看了别人的题解&#xff0c;才…

2-RACommand

RACommand RACCommand 就是命令 // RACCommand 就是命令// 0 创建一个CMD 穿进去一个用于构建RACSignal的Block参数来初始化RACommandRACCommand *cmd [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id _Nullable input) {// 此处是cmd 执行的输入源NSLog(…

玻璃上的编码喜悦(+ 10史诗般的Epigrams)

by Den McHenry丹麦克亨利(Den McHenry) 玻璃上的编码喜悦( 10史诗般的Epigrams) (Perlis on Coding Joy ( 10 Epic Epigrams)) Alan J. Perlis was the first recipient of the Turing Award. He’s possibly most remembered today for his Epigrams on Programming, which …

【Android】Activity生命周期(亲测)

测试手机&#xff1a;Nexus 5 系统&#xff1a;4.4 一、测试 测试代码&#xff1a; 1 package com.example.androidalarm;2 3 import android.app.Activity;4 import android.content.Context;5 import android.content.res.Configuration;6 import android.os.Bundle;7 impo…

angularjs 学习笔记 简单基础

angularjs是谷歌公司的一个项目&#xff0c;弥补了hml在构建方面的不足&#xff0c;通过指令&#xff08;directive&#xff09;来扩展html标签&#xff0c;可以使开发者使用html来声明动态内容。 angularjs主要用来开发单页应用&#xff08;SPA&#xff09;为主的项目。 angul…

3-RACSignal 常用方法

RACSingal的常用方法 一 基本使用 1map // 0 创建信号提供者// RACSubject&#xff0c;既能发送信号&#xff0c;又能订阅信号// 多用于代理&#xff0c;相当于OC里的delegate或者回调blockRACSubject *subject [RACSubject subject];// 1 绑定信号RACSignal *bindSignal …