threadlocal工具类定义
时间: 2023-10-19 11:35:28 浏览: 117
ThreadLocal是JDK提供的线程局部变量工具类。它定义了两个类型为ThreadLocalMap的变量:threadLocals和inherittableThreadLocals。threadLocals是用于存储线程本地变量的,它是ThreadLocal的独占变量。inherittableThreadLocals是可共享的变量,它也是ThreadLocalMap类型的,用于存储可被继承的线程本地变量。ThreadLocalMap是ThreadLocal的静态内部类,类似于Map的键值对存储结构。通过ThreadLocal的set方法将值添加到threadLocals中,并通过get方法获取当前线程变量中的threadLocals的值。如果需要移除值,则调用remove方法。ThreadLocal作为一个Key,用来让线程获取线程的局部变量。InheritableThreadLocal继承自ThreadLocal,并重写了getMap和createMap方法,唯一的区别是将ThreadLocal中的threadLocals换成了inherittableThreadLocals。这两个变量都是ThreadLocalMap类型,并且都是Thread类的属性。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [ThreadLocal详解](https://blog.csdn.net/zhangwei_david/article/details/125646858)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文