ThreadLocal 源码分析

ThreadLocal用于多线程环境下为每个线程提供独立的变量副本,确保线程间数据隔离。本文详细解析了ThreadLocal的工作原理,包括其内部实现机制、源码分析及应用场景。

Threadlocal 主要用于在多线程的情况下,每个线程可以保存一份本地的变量,该变量在线程之间相互独立,互不影响,并伴随着该线程的运行过程可以随时取出。

应用场景:比如,在web系统中,每个用户进行访问都有各自的不同的用户信息,可以在访问接收的时候做一个拦截器,把用户的信息放入到threadlocal中,在该线程的整个流程中,可以取出当前用户的相关信息,并且和其他线程不相互影响。

原理:每个线程都有一个threadlocalMap对象,在向threadlocal放入变量的时候,实际上是向每个线程的threadlocalMap中放入变量,放入的变量是key value形式的,其中key是当前的threadLocal对象,value是放入的变量。threadlocalMap中有个Entity的kev value 数组,该entity其中的key是弱引用,也就是存放threadLocal对象的地方。

源码分析

首先从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) {
            @SuppressWarnings("unchecked")
            T result = (T)e.value;
            return result;
        }
    }
    return setInitialValue();
}

(1)先获取当前的线程

(2)然后获取当前线程的ThreadLocalMap        ThreadLocalMap map = getMap(t);

如果是第一次调用该方法,之前没有set过值,那么map应该为null。会调用 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;
}

(1)初始化的时候value 是为null ,map根据上面第一次调用的话为null

(2)所有会调用createMap(t, value)方法

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

(1)获取当前线程threadLocalMap 然后new 了一个

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

(1)该构造方法为threadLocalMap内部的构造方法,主要的值存放在table数组中

(2)int i = firstKey.threadLocalHashCode & (INITIAL_CAPACITY - 1);  获取数组的下标,& (INITIAL_CAPACITY - 1)相当于对数组的初始容量取模,下标值一定是在数组容量范围内的。

(3)因为是第一次初始化,数组的大小为1.

(4) setThreshold(INITIAL_CAPACITY); 设置重新分配数组的空间的阀值,为数组大小的2/3,超过这个数组,从新分配空间。

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();
}

接下来看最开始的那段代码 ,假设不是第一次调用,当map不为空的时候 ,会走到

ThreadLocalMap.Entry e = map.getEntry(this); 这段代码

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

(1)if (e != null && e.get() == key)  如果当前位置值存在,并且key也相等,直接返回该值。

(2)存在一种情况,在放入值得时候,key的位置i已经存在了,则用线性探测法,去找下一个位置,直到找到不存在的位置,

所以上面的判断if不成立的时候,会调用getEntryAfterMiss(key, i, e),该方法用线性探测的方式查找值。

/**
 * Version of getEntry method for use when key is not found in
 * its direct hash slot.
 *
 * @param  key the thread local object
 * @param  i the table index for key's hash code
 * @param  e the entry at table[i]
 * @return the entry associated with key, or null if no such
 */
private Entry getEntryAfterMiss(ThreadLocal<?> key, int i, Entry e) {
    Entry[] tab = table;
    int len = tab.length;

    while (e != null) {
        ThreadLocal<?> k = e.get();
        if (k == key)
            return e;
        if (k == null)
            expungeStaleEntry(i);
        else
            i = nextIndex(i, len);
        e = tab[i];
    }
    return null;
}

(1)从 i的位置向下遍历整张表,因为table不会一定会有为null的位置,所以找不到则返回null,找到key相等的值则直接返回。

(2)因为key 为弱引用,所有当key为空的时候,调用方法expungeStaleEntry(i) 情况value的值,并且遍历整张表,清空所有key为null的值。

/**
 * Expunge a stale entry by rehashing any possibly colliding entries
 * lying between staleSlot and the next null slot.  This also expunges
 * any other stale entries encountered before the trailing null.  See
 * Knuth, Section 6.4
 *
 * @param staleSlot index of slot known to have null key
 * @return the index of the next null slot after staleSlot
 * (all between staleSlot and this slot will have been checked
 * for expunging).
 */
private int expungeStaleEntry(int staleSlot) {
    Entry[] tab = table;
    int len = tab.length;

    // expunge entry at staleSlot
    tab[staleSlot].value = null;
    tab[staleSlot] = null;
    size--;

    // Rehash until we encounter null
    Entry e;
    int i;
    for (i = nextIndex(staleSlot, len);
         (e = tab[i]) != null;
         i = nextIndex(i, len)) {
        ThreadLocal<?> k = e.get();
        if (k == null) {
            e.value = null;
            tab[i] = null;
            size--;
        } else {
            int h = k.threadLocalHashCode & (len - 1);
            if (h != i) {
                tab[i] = null;

                // Unlike Knuth 6.4 Algorithm R, we must scan until
                // null because multiple entries could have been stale.
                while (tab[h] != null)
                    h = nextIndex(h, len);
                tab[h] = e;
            }
        }
    }
    return i;
}

接下来我们看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);
}

(1)第一次set的时候同理上面已经说过createMap相关

(2)我们只看不是第一次的情况,这时候map不为空,该方法会调用map.set(this, value)

/**
 * 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();
}
(1)查找到有相同的key 则value进行覆盖,没有相同的key ,则在 i的位置创建一个新的entry

接下来看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);
 }

直接可以看m.remove(this)方法

/**
 * Remove the entry for key.
 */
private void remove(ThreadLocal<?> key) {
    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)]) {
        if (e.get() == key) {
            e.clear();
            expungeStaleEntry(i);
            return;
        }
    }
}

(1)直接遍历table,找到当前的key在table中位置

(2)e.clear() 把key 弱引用置为null

(3)expungeStaleEntry(i) 把所有key为null的 value值清空


ThreadLocal 可能写的不是很好,还有一些地方没有看的很明白,就没有在本文中细说。



纸张与塑料实例分割数据集 一、基础信息 • 数据集名称:纸张与塑料实例分割数据集 • 图片数量: 训练集:5304张图片 验证集:440张图片 总计:5744张图片 • 训练集:5304张图片 • 验证集:440张图片 • 总计:5744张图片 • 分类类别: 纸张(paper):常见的可回收材料,广泛用于包装和日常用品。 塑料(plastic):合成聚合物材料,在垃圾处理和回收中需准确识别。 • 纸张(paper):常见的可回收材料,广泛用于包装和日常用品。 • 塑料(plastic):合成聚合物材料,在垃圾处理和回收中需准确识别。 • 标注格式:YOLO格式,包含实例分割多边形标注,适用于实例分割任务。 • 数据格式:图片数据来源于相关领域,标注精确,支持模型训练。 二、适用场景 • 垃圾自动分类系统开发:数据集支持实例分割任务,帮助构建能够精确分割纸张和塑料物体的AI模型,用于智能垃圾桶、回收设施或环境监测系统。 • 环境监测与保护应用:集成至环保监控平台,实时检测和分类垃圾,促进垃圾分类、回收和可持续发展。 • 学术研究与创新:支持计算机视觉与环保领域的交叉研究,为垃圾识别和材料分类提供数据基础,推动AI在环境科学中的应用。 • 工业自动化与物流:在制造业或物流环节中,用于自动化检测和分类材料,提升生产效率和资源管理。 三、数据集优势 • 精准标注与实用性:每张图片均经过仔细标注,实例分割边界精确,确保模型能够学习纸张和塑料的细粒度特征。 • 数据多样性:涵盖多种场景和条件,提升模型在不同环境下的泛化能力和鲁棒性。 • 任务适配性强:标注兼容主流深度学习框架(如YOLO等),可直接用于实例分割模型训练,并支持扩展至其他视觉任务。 • 应用价值突出:专注于可回收材料检测,为垃圾管理、环保政策和自动化系统提供可靠数据支撑,助力绿色科技发展。
代码转载自:https://pan.quark.cn/s/fc36d9cf1917 《建筑工程施工强制性条文检查记录》是针对建筑工程施工过程中的核心环节进行合规性审核的关键性文件,其目的在于保障施工质量与施工安全。 这份文件收录了建筑工程施工过程中必须遵守的国家强制性准则、指令和技术规范,对于建筑施工作业单位、监理机构以及相关行政管理部门而言,均构成不可替代的参考资料。 建筑工程施工强制性条文主要涵盖以下几个方面的内容:1. **设计与施工准则**:工程项目的设计需符合国家的建筑设计准则,涵盖结构稳固性、防火性能、抗震性能、环保性能等方面的标准。 在施工作业阶段,必须严格依照设计图纸和施工计划进行,任何变更均需获得设计单位的一致许可。 2. **建筑材料品质**:所有投入使用的建筑材料,例如混凝土、钢筋、砌块等,都必须具备出厂合格证明,并接受第三方检测机构的品质验证。 严禁采用不合格或已过有效期的材料。 3. **施工安全措施**:在施工作业期间必须恪守安全生产准则,设置安全防护装置,例如脚手架、安全网、警示标识等。 施工人员需接受安全知识培训,并使用个人防护用品。 4. **环境管理**:施工作业应控制噪音、粉尘、废弃物等对环境可能造成的负面影响,推行绿色施工理念,采取降尘、防噪、废弃物分类处理等手段。 5. **工程质量监管**:每个施工作业阶段完成后,需实施自检、互检和专项检查,确保每一道工序的合格性。 对于基础工程、主体结构、防水工程等关键部位,应执行严格的验收流程。 6. **工程验收流程**:工程完工后,必须依照国家规范进行验收,涵盖单位工程验收、分部工程验收和整体工程验收,确保工程符合设计和使用需求。 7. **文档管理**:施工作业期间产生的技术文件、检测报告、会议记...
源码地址: https://pan.quark.cn/s/4916495967e3 在Android应用程序开发中,SharedPreferences(通常缩写为SP)是一种轻量级的数据存储解决方案,用于保存应用程序的简单配置信息,包括布尔值、整数、浮点数以及字符串等类型的数据。 SP基于XML文件格式,其存储位置位于设备的沙盒目录下,并且每个应用程序都拥有独立的SP文件,确保了应用间的数据隔离。 在某些使用场景下,可能会出现这样的情况:尽管数据已经成功保存在`SharedPreferences`中,但由于进程遭遇异常终止(例如系统强制关闭或通过内存清理工具触发),导致数据出现丢失或无法访问的问题。 本文旨在详细剖析这一现象,并提供相应的解决方法。 **问题的深入分析:**1. **进程终止的情况**:Android操作系统为了有效管理系统资源,可能会在特定条件下(比如内存资源紧张时)终止后台运行的应用进程。 一旦应用进程被终止,该进程内正在执行的所有任务,包括正在进行中的SP写入操作,都将被立即中断。 2. **非原子性写入操作**:`SharedPreferences.Editor`类提供的`commit()`或`apply()`方法并不具备原子性特征。 这意味着,如果在进程被终止之前,写入操作未能完整执行,那么数据可能无法成功持久化到磁盘存储中。 3. **异步操作的挑战**:`apply()`方法是一种异步操作,它会在后台线程中执行,且不保证立即将数据写入文件系统。 因此,如果在执行数据保存操作后紧接着进程被终止,那么所保存的数据可能还处于未写入状态。 **针对该问题的解决方案:**1. **优先选用`apply()`方法**:尽管`commit()`和`apply()`...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值