深入解析ThreadLocal
前置知识
- 具有一定的javase和javaweb基础
- 熟悉synchronized关键字
- 熟悉HashMap
- 熟悉 JDBC技术
1. ThreadLocal介绍1.1 官方介绍
/** * 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). * * 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. ** 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 threadId = *new ThreadLocal() { *@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(); *} * } * * 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). * * @authorJosh Bloch and Doug Lea * @since1.2 */public class ThreadLocal {...
从Java官方文档中的描述:ThreadLocal类用来提供线程内部的局部变量 。 这种变量在多线程环境下访问(通过get和set方法访问)时能保证各个线程的变量相对独立于其他线程内的变量 。 ThreadLocal实例通常来说都是private static类型的 , 用于关联线程和线程上下文 。我们可以得知 ThreadLocal 的作用是:提供线程内的局部变量 , 不同的线程之间不会相互干扰 , 这种变量在线程的生命周期内起作用 , 减少同一个线程内多个函数或组件之间一些公共变量传递的复杂度 。
总结:1. 线程并发: 在多线程并发的场景下2. 传递数据: 我们可以通过ThreadLocal在同一线程 , 不同组件中传递公共变量3. 线程隔离: 每个线程的变量都是独立的 , 不会互相影响
1.2 基本使用1.2.1 常用方法在使用之前,我们先来认识几个ThreadLocal的常用方法data-draft-node="block" data-draft-type="table" data-size="normal" data-row-style="normal">
方法声明描述
1.2.2 使用案例我们来看下面这个案例 , 感受一下ThreadLocal 线程隔离的特点:
public class MyDemo {private String content;private String getContent() {return content;}?private void setContent(String content) {this.content = content;}?public static void main(String[] args) {MyDemo demo = new MyDemo();for (int i = 0; i < 5; i++) {Thread thread = new Thread(new Runnable() {@Overridepublic void run() {demo.setContent(Thread.currentThread().getName() + "的数据");System.out.println("-----------------------");System.out.println(Thread.currentThread().getName() + "--->" + demo.getContent());}});thread.setName("线程" + i);thread.start();}}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 国家工业互联网标识解析二级节点(威海)上线
- 全新8核国产CPU深入探秘:马上能买到
- 多就是好吗?解析智能手机多摄发展困局
- 新基建下,系统集成商数字化建设及渠道管理深度解析
- 四个维度解析区域发展与治理
- 智媒视角看深圳用数据解析城市
- “联邦的战斗力量”再现,华硕Z490主板机动战士高达版解析
- 天翼云全国首个工业互联网平台二级解析节点上线
- 16G运存+256G内存,专业骁龙865旗舰,性价比深入人心
- 深入理解Netty编解码、粘包拆包、心跳机制