Android源码解析Handler系列第(二)篇--- ThreadLocal详解

2024-09-02 15:08

本文主要是介绍Android源码解析Handler系列第(二)篇--- ThreadLocal详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在上篇文章Android源码解析Handler系列第(一)篇说了Message的内部维持的全局池机制。这一篇仍然是准备知识,因为在Handler中有ThreadLocal的身影,大家知道,Handler创建的时候会采用当前线程的Looper来构造消息循环系统,那么Handler内部如何获取到当前线程的Looper呢?这就要使用ThreadLocal了,ThreadLocal可以在不同的线程之中互不干扰地存储并提供数据,通过ThreadLocal可以轻松获取每个线程的Looper,所以,ThreadLocal是理解Looper的关键之一。

先看一下官方对这个类的解释

/*** Implements a thread-local storage, that is, a variable for which each thread* has its own value. All threads share the same {@code ThreadLocal} object,* but each sees a different value when accessing it, and changes made by one                                                      * thread do not affect the other threads. The implementation supports* {@code null} values.** @see java.lang.Thread                                                                                                                                                                                                                                                                                   * @author Bob Lee*/

大概意思就是说:实现了一个线程本地存储,也就是说,每个线程的一个变量,有自己的值。所有线程共享同一个 ThreadLocal对象,但每个线程访问它时,看到一个不同的值,如果一个线程改变了这个值,不影响其他线程,ThreadLocal支持NULL值。理解好费劲,只能说我翻译的太差!

重新解释一下:当工作于多线程中的对象使用ThreadLocal 维护变量时,ThreadLocal 为 每个使用该变量的线程分配一个独立的变量副本。每个线程独立改变自己的副本,而不影响其他线程所对应的变量副本。这就是它的基本原理,ThreadLocal主要的 API很简单。

public void set(T value):将值放入线程局部变量中

public T get():从线程局部变量中获取值

public void remove():从线程局部变量中移除值(有助于 JVM 垃圾回收)

protected T initialValue():返回线程局部变量中的初始值(默认为 null)

ThreadLocal为各个线程保存一个变量副本,这句话是关键,怎么理解?先来一个简单的DEMO

public class ThreadLocalTest {public static void main(String[] args) throws InterruptedException {ThreadLocal<Boolean> mBooleanThreadLocal=new ThreadLocal<Boolean>(){@Overrideprotected Boolean initialValue() {//初始值是falsereturn false;}};mBooleanThreadLocal.set(true);System.out.println("[Thread#main]mBooleanThreadLocal=" +mBooleanThreadLocal.hashCode()+"  "+ mBooleanThreadLocal.get());new Thread("Thread#1") {@Overridepublic void run() {mBooleanThreadLocal.set(false);System.out.println( "[Thread#1]mBooleanThreadLocal="+mBooleanThreadLocal.hashCode() +"  "+ mBooleanThreadLocal.get());};}.start();new Thread("Thread#2") {@Overridepublic void run() {System.out.println( "[Thread#2]mBooleanThreadLocal="+mBooleanThreadLocal.hashCode() + "  "+mBooleanThreadLocal.get());};}.start();}
}
输出结果:[Thread#main]mBooleanThreadLocal=366712642      true
[Thread#1]mBooleanThreadLocal=366712642      false
[Thread#2]mBooleanThreadLocal=366712642      false

在上面的代码中,在主线程中设置mBooleanThreadLocal的值为true,在子线程1中设置mBooleanThreadLocal的值为false,在子线程2中不设置mBooleanThreadLocal的值,然后分别在3个线程中通过get方法去mBooleanThreadLocal的值,所以,主线程中应该是true,子线程1中应该是false,而子线程2中由于没有设置值,所以应该是初始值false。

再看一个DEMO

public class ThreadLocalTest {//创建一个Integer型的线程本地变量public static final ThreadLocal<Integer> local = new ThreadLocal<Integer>() {@Overrideprotected Integer initialValue() {return 0;}};public static void main(String[] args) throws InterruptedException {Thread[] threads = new Thread[5];for (int j = 0; j < 5; j++) {       threads[j] = new Thread(new Runnable() {@Overridepublic void run() {//获取当前线程的本地变量,然后累加5次int num = local.get();for (int i = 0; i < 5; i++) {num++;}//重新设置累加后的本地变量local.set(num);System.out.println(Thread.currentThread().getName() + " : "+ local.get());}}, "Thread-" + j);}for (Thread thread : threads) {thread.start();}}
}
输出结果:Thread-1 : 5
Thread-3 : 5
Thread-4 : 5
Thread-0 : 5
Thread-2 : 5

开了5个线程,每个线程累加后的结果都是5,各个线程处理自己的本地变量值,线程之间互不影响。

读到这里,相信你对ThreadLocal的作用已经了解了,ThreadLocal和synchronized是有区别的,概括起来说,对于多线程资源共享的问题,同步机制采用了“以时间换空间”的方式,而ThreadLocal采用了“以空间换时间”的方式。前者仅提供一份变量,让不同的线程排队访问,而后者为每一个线程都提供了一份变量,因此可以同时访问而互不影响,所以ThreadLocal和synchronized都能保证线程安全,但是应用场景却大不一样。

现在从源码中去找找答案,为什么各个线程都能保留一份副本,做到多并发的时候,线程互不影响呢?

ThreadLocal的构造函数是空的,啥也没有。

public ThreadLocal() {
}

看一下里面的set方法

    /*** Sets the value of this variable for the current thread. If set to* {@code null}, the value will be set to null and the underlying entry will* still be present.** @param value the new value of the variable for the caller thread.*/public void set(T value) {Thread currentThread = Thread.currentThread();Values values = values(currentThread);if (values == null) {values = initializeValues(currentThread);}values.put(this, value);}

首先获得了当前线程实例,将实例传入values方法中,获得一个Values对象,第一次获得的Values对象是空的,就调用initializeValues初始化一个values对象,最后把当前对象作为key,value作为值,放进values中。现在的重点就是搞懂Values是什么?Values原来是ThreadLocal的静态内部类。在ThreadLocal.Values中有一个table成员,而这个table就以key,value的形式存储了线程的本地变量。key是ThreadLocal类型的对象的弱引用,而Value则是线程需要保存的线程本地变量T。

 /** * 存放数据的数组。他存放数据的形式有点像map,是ThreadLocal与value相对应, 长度总是2的N次方*/private Object[] table;

table表结构

Value的主要API有下面几个
- void put(ThreadLocal

 /*** Sets entry for given ThreadLocal to given value, creating an* entry if necessary.*/void put(ThreadLocal<?> key, Object value) {cleanUp();// Keep track of first tombstone. That's where we want to go back// and add an entry if necessary.int firstTombstone = -1;for (int index = key.hash & mask;; index = next(index)) {Object k = table[index];if (k == key.reference) {// Replace existing entry.table[index + 1] = value;return;}if (k == null) {if (firstTombstone == -1) {// Fill in null slot.table[index] = key.reference;table[index + 1] = value;size++;return;}// Go back and replace first tombstone.table[firstTombstone] = key.reference;table[firstTombstone + 1] = value;tombstones--;size++;return;}// Remember first tombstone.if (firstTombstone == -1 && k == TOMBSTONE) {firstTombstone = index;}}}

上面是ThreadLocal存的过程,现在看取的过程。

    /*** Returns the value of this variable for the current thread. If an entry* doesn't yet exist for this variable on this thread, this method will* create an entry, populating the value with the result of* {@link #initialValue()}.** @return the current value of the variable for the calling thread.*/@SuppressWarnings("unchecked")public T get() {// Optimized for the fast path.Thread currentThread = Thread.currentThread();Values values = values(currentThread);if (values != null) {Object[] table = values.table;int index = hash & values.mask;if (this.reference == table[index]) {return (T) table[index + 1];}} else {values = initializeValues(currentThread);}return (T) values.getAfterMiss(this);}

首先取出当前线程的Values对象,跟set方法一样,如果这个对象为null那么就返回初始值, 如果Values对象不为null,那就取出它的table数组并找出ThreadLocal的reference对象在table数组中的位置,然后table数组中的下一个位置所存储的数据就是ThreadLocal的值。从可以看出,ThreadLocal的set和get方法所操作的对象都是当前线程的Values对象的table数组,如果一个变量使用了ThreadLocal,通过ThreadLocal的set和get方法,每一个线程的都会有一份这个变量的副本(键值对的形式进行存取),这就是为什么多线程并发的时候,线程互不影响的操作一个变量。

最后在举一个ThreadLocal的应用的例子:JDBC连接mysql数据库,把 Connection 放到了 ThreadLocal 中,这样每个线程之间就隔离了,不会相互干扰。

public class DBUtil {// 数据库配置private static final String driver = "com.mysql.jdbc.Driver";private static final String url = "jdbc:mysql://localhost:3306/demo";private static final String username = "xxx";private static final String password = "xxx";// 定义一个用于放置数据库连接的局部线程变量(使每个线程都拥有自己的连接)private static ThreadLocal<Connection> connContainer = new ThreadLocal<Connection>();// 获取连接public static Connection getConnection() {Connection conn = connContainer.get();try {if (conn == null) {Class.forName(driver);conn = DriverManager.getConnection(url, username, password);}} catch (Exception e) {e.printStackTrace();} finally {connContainer.set(conn);}return conn;}// 关闭连接public static void closeConnection() {Connection conn = connContainer.get();try {if (conn != null) {conn.close();}} catch (Exception e) {e.printStackTrace();} finally {connContainer.remove();}}
}

Please accept mybest wishes for your happiness and success

参考链接:
https://my.oschina.net/clopopo/blog/149368
http://blog.csdn.net/singwhatiwanna/article/details/48350919
https://my.oschina.net/huangyong/blog/159725

这篇关于Android源码解析Handler系列第(二)篇--- ThreadLocal详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1130304

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客