博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ThreadLocal详解
阅读量:4072 次
发布时间:2019-05-25

本文共 6685 字,大约阅读时间需要 22 分钟。

使用ThreadLocal的原因

在多线程访问的时候,为了解决线程安全问题,使用 synchronized 关键字来实现线程同步的可以解决多线程并发访问的安全问题,但是在这种解决方案存在有性能问题,多个线程访问到的都是同一份变量的内容,在多线程同时访问的时候每次只允许一个线程读取变量内容,对变量值进行访问或者修改,其他线程只能处于排队等候状态,顺序执行,谁先抢占到系统资源谁先执行,导致系统效率低下。这是一种以延长访问时间来换取线程安全性的策略。简单来说就是以时间长度换取线程安全,在多用户并发访问的时候,由于等待时间太长,这对用户来说是不能接受的。

而使用 ThreadLocal 类,该类在每次实例化创建线程的时候都为每一个线程在本地变量中创建了自己独有的变量副本。每个线程都拥有了自己独立的一个变量,竞争条件被彻底消除了,那就没有必要使用 synchronized 关键字对这些线程进行同步,它们也能最大限度的使用系统资源,由CPU调度并发执行。并且由于每个线程在访问该变量时,读取和修改的,都是自己独有的那一份变量拷贝副本,不会对其他的任何副本产生影响,并发错误出现的可能也完全消除了。对比前一种方案,这是一种以空间来换取线程安全性的策略。在效率上来说比同步高了很多,可以应对多线程并发访问。

通过查看ThreadLocal类源码,该类中提供了两个主要的方法 get()set(),还有一个用于回收本地变量中的方法remove()也是常用的如下:

下面是 set() 方法的源码:

/**     * 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);    }

set() 中通过 getMap(Thread t)方法获取一个和当前线程相关的 ThreadLocalMap,然后将变量的值设置到这个

ThreadLocalMap对象中,如果获取到的 ThreadLocalMap 对象为空,就通过createMap()方法创建。

线程隔离的秘密,就在于 ThreadLocalMap这个类。ThreadLocalMapThreadLocal类的一个静态内部类,它实现了键值对的设置和获取(类似于 Map<K,V> 存储的key-value),每个线程中都有一个独立的ThreadLocalMap副本,它所存储的值,只能被当前线程读取和修改。ThreadLocal类通过操作每一个线程特有的 ThreadLocalMap 副本,从而实现了变量访问在不同线程中实现隔离。因为每个线程的变量都是自己特有的,完全不会有并发错误。还有一点就是,ThreadLocalMap存储的键值对中的键是this对象指向的ThreadLocal对象,而值就是你所设置的对象了。

来分析源码中出现的getMap和createMap方法的实现:

/**     * 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     */    void createMap(Thread t, T firstValue) {        t.threadLocals = new ThreadLocalMap(this, firstValue);    }

通过源码分析可以看出,通过获取和设置 Thread内的 threadLocals变量,而这个变量的类型就是 ThreadLocalMap,这样进一步验证了上文中的观点:每个线程都有自己独立的ThreadLocalMap对象。打开java.lang.Thread类的源代码,我们能得到更直观的证明:

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

下面来看一下 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) {                @SuppressWarnings("unchecked")                T result = (T)e.value;                return result;            }        }        return setInitialValue();    }    /**     * Variant of set() to establish initialValue. Used instead     * of set() in case user has overridden the set() method.     *     * @return the initial value     */    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;    }

通过以上源码的分析,在获取和当前线程绑定的值时,ThreadLocalMap对象是以 this 指向的 ThreadLocal 对象为键进行查找的,set() 方法是设置变量的拷贝副本,get() 方法通过键值对的方式获取到这个本地变量的副本的value

remove() 源码分析:

/**     * Removes the current thread's value for this thread-local     * variable.  If this thread-local variable is subsequently     * {@linkplain #get read} by the current thread, its value will be     * reinitialized by invoking its {@link #initialValue} method,     * unless its value is {@linkplain #set set} by the current thread     * in the interim.  This may result in multiple invocations of the     * {@code initialValue} method in the current thread.     *     * @since 1.5     */     public void remove() {         ThreadLocalMap m = getMap(Thread.currentThread());         if (m != null)             m.remove(this);     }

通过源码可以知道,该方法就是通过 this 找到ThreadLocalMap 中保存的变量副本做回收处理。

具体实现例子(实现数据库的连接关闭)

下面看一个使用 ThreadLocal 实现的数据库 Connection 连接:

package cn.czl.util.dbc;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/** * 为了更加方便的传输关键的引用对象(Connection),所以在本类之中将利用ThreadLocal进行数据库连接的保存 * 这个类中的ThreadLocal应该作为一个公共的数据,公共的数据使用static声明 * @author czl */public class DatabaseConnection  {
private static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ; private static final String DBURL = "jdbc:oracle:thin:@localhost:1521:orcl" ; private static final String USER = "scott" ; private static final String PASSWORD = "tiger" ; private static ThreadLocal
threadLocal = new ThreadLocal
() ; /** * 取得数据库的连接对象,如果在取得的时候没有进行连接则自动创建新的连接 * 但是需要防止同一个线程可能重复调用此操作的问题 * @return */ public static Connection getConnection() { Connection conn = threadLocal.get() ; // 首先判断一下在ThreadLocal里面是否保存有当前连接对象 if (conn == null) { // 如果此时的连接对象是空,那么就表示没有连接过,则创建一个新的连接 conn = rebuildConnection() ; // 创建新的连接 threadLocal.set(conn); // 保存到ThreadLocal之中,以便一个线程执行多次数据库的时候使用 } return conn ; // 返回连接对象 } /** * 重新建立新的数据库连接 * @return Connection接口对象 */ private static Connection rebuildConnection() { // 创建新的连接对象 try { Class.forName(DBDRIVER) ; return DriverManager.getConnection(DBURL,USER,PASSWORD); } catch (Exception e) { e.printStackTrace(); } return null ; } /** * 关闭数据库连接 */ public static void close() { System.out.println(Thread.currentThread().getName() + " 关闭数据库连接。"); Connection conn = threadLocal.get() ; // 取得数据库连接 if (conn != null) { try { conn.close(); threadLocal.remove(); // 从ThreadLocal中删除掉保存的数据 } catch (SQLException e) { e.printStackTrace(); } } }}

当用户需要调用连接数据库的时候,只需要通过 DatabaseConnection.getConnection()连接数据库,在实现业务逻辑的时候,每次调用都会为调用处创建一个连接数据库的线程副本,每次所作的修改互不干扰,在多用户并发访问使用的时候,很好的避免了使用同步带来的性能问题。

转载地址:http://ndrji.baihongyu.com/

你可能感兴趣的文章
adb server version (39) doesn't match this client (40); killing...
查看>>
adb server version (39) doesn't match this client (40); killing...
查看>>
Unity高级游戏地编案例
查看>>
UE4地编大型开放世界~制作烘焙全流程
查看>>
TextMesh Pro不能显示中文的解决办法是创建字贴图,常用汉字3500
查看>>
permanently
查看>>
Unity2019游戏框架搭建第一季C# 核心知识与简易框架搭建 + Unity2019 游戏框架搭建第二季:UI 模块与资源模块持续精进...
查看>>
Unity发布到Google Play应用上架流程
查看>>
50 个 Chrome Developer Tools 必备技巧
查看>>
TextMesh Pro不能显示中文的解决办法是创建字贴图,常用汉字3500+特殊字符
查看>>
unity3d钢琴游戏完整项目源码
查看>>
Unity 2019 LTS正式推出
查看>>
关于UE4使用中虚幻商城保管库的目录问题
查看>>
Unity3D 2018版本 Post Process 后期处理插件使用介绍
查看>>
UE4户外森林场景全流程教学
查看>>
零基础入门Unity - 古迹探险(Unity2017)
查看>>
C#批量修改文件后缀名
查看>>
Unity3D普通开发人员,U3D主程分别需要掌握的技能
查看>>
关于XMLList = node["节点1"]["节点2"]中只有1个节点的问题
查看>>
readUnsignedInt () 自动移动字节流位置,和.net是一样的
查看>>