多线程与并发之ThreadLocal

一 什么是ThreadLocal?

源码注释:

/**
 * This class provides thread-local variables.  These variables differ from
 * their normal counterparts in that each thread that accesses one (via its
 * {@code get} or {@code set} method) has its own, independently initialized
 * copy of the variable.  {@code ThreadLocal} instances are typically private
 * static fields in classes that wish to associate state with a thread (e.g.,
 * a user ID or Transaction ID).
 *
 * <p>For example, the class below generates unique identifiers local to each
 * thread.
 * A thread's id is assigned the first time it invokes {@code ThreadId.get()}
 * and remains unchanged on subsequent calls.
 * <pre>
 * import java.util.concurrent.atomic.AtomicInteger;
 *
 * public class ThreadId {
 *     // Atomic integer containing the next thread ID to be assigned
 *     private static final AtomicInteger nextId = new AtomicInteger(0);
 *
 *     // Thread local variable containing each thread's ID
 *     private static final ThreadLocal&lt;Integer&gt; threadId =
 *         new ThreadLocal&lt;Integer&gt;() {
 *             &#64;Override protected Integer initialValue() {
 *                 return nextId.getAndIncrement();
 *         }
 *     };
 *
 *     // Returns the current thread's unique ID, assigning it if necessary
 *     public static int get() {
 *         return threadId.get();
 *     }
 * }
 * </pre>
 * <p>Each thread holds an implicit reference to its copy of a thread-local
 * variable as long as the thread is alive and the {@code ThreadLocal}
 * instance is accessible; after a thread goes away, all of its copies of
 * thread-local instances are subject to garbage collection (unless other
 * references to these copies exist).
 *
 * @author  Josh Bloch and Doug Lea
 * @since   1.2
 */

翻译:ThreadLocal提供线程局部变量,ThreadLocal中通过set方法填充的变量属于当前线程,该变量对其他线程隔离,ThreadLocal为变量在每个线程中都创建一个副本。ThreadLocal实例通常是私有的静态变量,其希望将一些状态值(用户id,事务id)与线程关联。只要线程还活着并且可访问,每个线程持有一个指向变量副本的弱引用,线程消失之后,除非其他引用依然指向这些变量副本,否则所有线程变量副本将会被垃圾回收。

二 threadlocal的使用场景

spring采用threadlocal的方式来保证单个线程中数据库操作使用的是同一个数据连接

1、在进行对象跨层传递的时候,使用ThreadLocal可以避免多次传递,打破层次间的约束。

2、线程间数据隔离

3、进行事务操作,用于存储线程事务信息。

4、数据库连接,Session会话管理。

三 threadlocal使用

public class ThreadLocalTest {

    private static ThreadLocal<String> local = new ThreadLocal<>();


    public static void main(String[] args) {
        local.set("lizahngjun");

        String s = local.get();
        System.out.println(s);

        local.remove();

        String s1 = local.get();
        System.out.println(s1);
    }
}

四 源码分析

(1)查看Thread源码

可以看到每个线程都维护了自己的threadLocals变量,当每个线程创建ThreadLocal时,实际上数据是存在Thread的threadLocals变量里面,其他线程是无法访问这个threadLocals变量的。

(2)ThreadLocalMap结构

 

(3)set方法

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

                if (k == key) {
                    e.value = value;
                    return;
                }

                if (k == null) {
                    replaceStaleEntry(key, value, i);
                    return;
                }
            }

            tab[i] = new Entry(key, value);
            int sz = ++size;
            if (!cleanSomeSlots(i, sz) && sz >= threshold)
                rehash();
        }

由源码可知,每个ThreadLocal对象都有一个threadLocalHashCode,通过ThreadLocal对象的hash值,定位到table中的位置i,如果位置i为空,则初始化一个entry对象放在该位置上,如果位置i不为空且key相同,则刷新value,如果位置i不为空且key不同,则找下一个空位置,直到为空为止。

(4)ThreadLocal的实例以及其值是否放在线程的私有内存中???

ThreadLocal的实例以及其值依然放在堆内中,只是通过一些方法将其修改成了线程可见。

(5)如何共享线程的ThreadLocal数据???

使用InheritableThreadLocal可以实现多个线程访问ThreadLocal的值。

(6)共享线程的ThreadLocal数据是如何传递的???

Thread初始化创建的时候,如果线程的inheritThreadLocals变量不为空,而且父线程的inheritThreadLocals也存在,那么就把父线程的inheritThreadLocals给当前线程的inheritThreadLocals。

(7)内存泄漏

原因:发生内存泄露的主要原因是entry的key是弱引用,而value是强引用,当垃圾回收器扫描到内存中的弱引用时,不管当前内存空间是否充足,都会回收这些弱引用,而如果创建ThreadLocal的线程一直持续运行,那么entry对象中的value一直得不到回收,则会造成内存泄漏。例如线程池中的线程会复用,由于复用的目的线程依然存活,threadlocal设定的value值会被一直持有,导致内存泄漏。

解决:在使用的最后用remove把值清空。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值