<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F</id>
	<title>ThreadLocal：线程局部变量 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F"/>
	<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;action=history"/>
	<updated>2026-05-09T15:29:45Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;diff=6398&amp;oldid=prev</id>
		<title>Eijux：​/* 源码相关 */</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;diff=6398&amp;oldid=prev"/>
		<updated>2022-12-31T20:19:55Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;源码相关&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2023年1月1日 (日) 04:19的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l18&quot;&gt;第18行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第18行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;threadLocals&amp;lt;/syntaxhighlight&amp;gt;'''（类型：'''ThreadLocal.ThreadLocalMap'''）用于维护“'''线程本地变量'''”。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;threadLocals&amp;lt;/syntaxhighlight&amp;gt;'''（类型：'''ThreadLocal.ThreadLocalMap'''）用于维护“'''线程本地变量'''”。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''ThreadLocal'''&amp;lt;/span&amp;gt; 类：&amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;'''当前线程的 ThreadLocalMap 的访问入口'''&amp;lt;/span&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''ThreadLocal'''&amp;lt;/span&amp;gt; 类：&amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;'''当前线程的 ThreadLocalMap 的访问入口'''&amp;lt;/span&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;#* &amp;lt;span style=&quot;color: red&quot;&gt;'''ThreadLocal 对象通常被定义为“private static”'''&amp;lt;/span&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* 属性：&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* 属性：&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# 每个 ThreadLocal 对象都包含了一个独一无二的 '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;threadLocalHashCode&amp;lt;/syntaxhighlight&amp;gt;'''；&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# 每个 ThreadLocal 对象都包含了一个独一无二的 '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;threadLocalHashCode&amp;lt;/syntaxhighlight&amp;gt;'''； &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt; ——【'''用于计算 ThreadLocal 对象在 ThreadLocalMap.table 中的位置'''】&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* 方法：&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* 方法：&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;set(T value)&amp;lt;/syntaxhighlight&amp;gt;'''：设置“线程本地变量”的值，将调用 ThreadLocalMap 的 '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;set(ThreadLocal&amp;lt;?&amp;gt; key, Object value)&amp;lt;/syntaxhighlight&amp;gt;'''；&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;set(T value)&amp;lt;/syntaxhighlight&amp;gt;'''：设置“线程本地变量”的值，将调用 ThreadLocalMap 的 '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;set(ThreadLocal&amp;lt;?&amp;gt; key, Object value)&amp;lt;/syntaxhighlight&amp;gt;'''；&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;T get()&amp;lt;/syntaxhighlight&amp;gt;'''：获取“线程本地变量”的值，将调用 ThreadLocalMap 的 '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;getEntry(ThreadLocal&amp;lt;?&amp;gt; key)&amp;lt;/syntaxhighlight&amp;gt;'''；&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;T get()&amp;lt;/syntaxhighlight&amp;gt;'''：获取“线程本地变量”的值，将调用 ThreadLocalMap 的 '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;getEntry(ThreadLocal&amp;lt;?&amp;gt; key)&amp;lt;/syntaxhighlight&amp;gt;'''；&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;remove()&amp;lt;/syntaxhighlight&amp;gt;'''：删除此“线程本地变量”，将调用 ThreadLocalMap 的 '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;remove(ThreadLocal&amp;lt;?&amp;gt; key)&amp;lt;/syntaxhighlight&amp;gt;'''&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;ref neme=&amp;quot;Entry&amp;amp;WeakReference&amp;quot;/&amp;gt;&lt;/del&gt;；&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;remove()&amp;lt;/syntaxhighlight&amp;gt;'''&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;ref name=&amp;quot;ThreadLocal.remove&amp;quot;/&amp;gt;&lt;/ins&gt;：删除此“线程本地变量”，将调用 ThreadLocalMap 的 '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;remove(ThreadLocal&amp;lt;?&amp;gt; key)&amp;lt;/syntaxhighlight&amp;gt;'''；&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#**【&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;'''泛型类，所以“线程本地变量”的值可以为各种类型'''&amp;lt;/span&amp;gt;】&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#**【&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;'''泛型类，所以“线程本地变量”的值可以为各种类型'''&amp;lt;/span&amp;gt;】&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''ThreadLocalMap'''&amp;lt;/span&amp;gt; &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;类：&lt;/del&gt;&amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;'''存储了一组“K-V”映射'''（其实质是一个 '''Entry 数组'''）&amp;lt;/span&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''ThreadLocalMap'''&amp;lt;/span&amp;gt; &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;类&amp;lt;ref name=&amp;quot;ThreadLocalMap对象的清理&amp;quot;/&amp;gt;：&lt;/ins&gt;&amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;'''存储了一组“K-V”映射'''（其实质是一个 '''Entry 数组'''）&amp;lt;/span&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* '''ThreadLocal''' 的内部类；&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* '''ThreadLocal''' 的内部类；&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* 属性：&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* 属性：&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l34&quot;&gt;第34行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第35行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;remove(ThreadLocal&amp;lt;?&amp;gt; key)&amp;lt;/syntaxhighlight&amp;gt;'''：从 table 中移除“ThreadLocal 对象”关联的 Entry；&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;remove(ThreadLocal&amp;lt;?&amp;gt; key)&amp;lt;/syntaxhighlight&amp;gt;'''：从 table 中移除“ThreadLocal 对象”关联的 Entry；&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''Entry'''&amp;lt;/span&amp;gt; 类：&amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;'''一个 Entry 就是一个“线程本地变量 -&amp;gt; 线程本地变量的值”的映射'''&amp;lt;/span&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# &amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''Entry'''&amp;lt;/span&amp;gt; 类：&amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;'''一个 Entry 就是一个“线程本地变量 -&amp;gt; 线程本地变量的值”的映射'''&amp;lt;/span&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* '''ThreadLocalMap''' 的内部类，继承于：“'''WeakReference'''”&amp;lt;ref &lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;neme&lt;/del&gt;=&amp;quot;Entry&amp;amp;WeakReference&amp;quot;/&amp;gt;；&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* '''ThreadLocalMap''' 的内部类，继承于：“'''WeakReference'''”&amp;lt;ref &lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;name&lt;/ins&gt;=&amp;quot;Entry&amp;amp;WeakReference&amp;quot;/&amp;gt;；&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* 属性：&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#* 属性：&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;referent&amp;lt;/syntaxhighlight&amp;gt;'''：（从 Reference 类中继承而来）保存“ThreadLocal对象”&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#*# '''&amp;lt;syntaxhighlight lang=&amp;quot;Java&amp;quot; inline&amp;gt;referent&amp;lt;/syntaxhighlight&amp;gt;'''：（从 Reference 类中继承而来）保存“ThreadLocal对象”&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;diff=6397&amp;oldid=prev</id>
		<title>Eijux：​/* 参考 */</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;diff=6397&amp;oldid=prev"/>
		<updated>2022-12-31T20:15:29Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;参考&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2023年1月1日 (日) 04:15的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l419&quot;&gt;第419行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第419行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#【变量隔离】：就是用于实现“&amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''线程安全'''&amp;lt;/span&amp;gt;”。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#【变量隔离】：就是用于实现“&amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''线程安全'''&amp;lt;/span&amp;gt;”。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#【变量传递】：参考【'''[[Hibernate笔记_4：核心知识#关于“Session_管理方式：绑定到本地线程”]]'''】（将业务层获取的 Session 传递到 DAO 层）。&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;#【变量传递】：参考【'''[[Hibernate笔记_4：核心知识#关于“Session_管理方式：绑定到本地线程”]]'''】（将业务层获取的 Session 传递到 DAO 层）。&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/ref&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;ref name=&quot;Entry&amp;amp;WeakReference&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;span style=&quot;color: blue&quot;&gt;'''关于&amp;lt;syntaxhighlight lang=&quot;Java&quot; inline&gt;Entry extends WeakReference&amp;lt;ThreadLocal&amp;lt;?&gt;&gt;&amp;lt;/syntaxhighlight&gt;'''&amp;lt;/span&gt;：&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;blockquote&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;'''WeakReference''' 标志性的特点是：&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br/&gt;   '''不会影响到被应用对象的 GC 回收行为'''（即，只要对象被除 WeakReference 对象之外所有的对象解除引用后，该对象便可以被 GC 回收），&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;br/&gt;   只不过在被对象回收之后，WeakReference 实例获取被引用对象时会返回 '''null'''。&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/blockquote&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;: &amp;lt;span style=&quot;color: blue&quot;&gt;'''为什么 Entry 继承 WeakReference？'''&amp;lt;/span&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;:: &amp;lt;span style=&quot;color: green&quot;&gt;'''ThreadLocal 对象的 GC 回收'''&amp;lt;/span&gt;：如果使用“强引用”，ThreadLocal 对象在使用完毕，将会由于被 Entry 对象“强引用”所引用导致无法被 GC 回收。&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/ref&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;ref name=&quot;ThreadLocalMap对象的清理&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;span style=&quot;color: blue&quot;&gt;'''ThreadLocalMap 对象的清理'''&amp;lt;/span&gt;：&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;: ThreadLocalMap 的“get、set、remove”等方法中，会调用“expungeStaleEntry、replaceStaleEntry、cleanSomeSlots”等方法（根据“&amp;lt;syntaxhighlight lang=&quot;Java&quot; inline&gt;if (e != null &amp;amp;&amp;amp; e.get() == null)&amp;lt;/syntaxhighlight&gt;”判断 Entry 所引用的 ThreadLocal 对象是否还存在）来对 table 进行清理，以及时释放内存。&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;/ref&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;ref name=&quot;ThreadLocal.remove&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;span style=&quot;color: blue&quot;&gt;'''为什么要求“ThreadLocalMap 对象使用完毕后及时调用 remove()？”'''&amp;lt;/span&gt;：&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;# 在使用线程池的时候，线程执行完毕放回线程池中，有可能在下一次执行时带入上一次的执行状态。&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;# 便于对线程的 ThreadLocalMap 对象的 table 及时清理，以释放内存。&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/ref&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/ref&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/references&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/references&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;diff=6396&amp;oldid=prev</id>
		<title>Eijux：​/* 结构关系 */</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;diff=6396&amp;oldid=prev"/>
		<updated>2022-12-31T19:06:11Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;结构关系&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;a href=&quot;http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;amp;diff=6396&amp;amp;oldid=6395&quot;&gt;显示更改&lt;/a&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;diff=6395&amp;oldid=prev</id>
		<title>Eijux：​创建页面，内容为“category:Java  == 关于 ==  &lt;span style=&quot;color: blue; font-size: 150%&quot;&gt;'''ThreadLocal'''&lt;/span&gt; 关键字的概念与作用，在&lt;u&gt;“'''Java 核心技术'''”（卷 I）的“'''并发'''”部分&lt;/u&gt;&lt;ref name=&quot;并发&quot;/&gt;与&lt;u&gt;“'''深入理解 JVM 虚拟机'''”的“'''线程安全'''”部分&lt;/u&gt;&lt;ref name=&quot;线程安全&quot;/&gt;已经了解差不多了。      简单来说：'''ThreadLocal 就是通过定义“线程局部变量”的方式实现“线程…”</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ThreadLocal%EF%BC%9A%E7%BA%BF%E7%A8%8B%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F&amp;diff=6395&amp;oldid=prev"/>
		<updated>2022-12-29T17:49:31Z</updated>

		<summary type="html">&lt;p&gt;创建页面，内容为“&lt;a href=&quot;/%E5%88%86%E7%B1%BB:Java&quot; title=&quot;分类:Java&quot;&gt;category:Java&lt;/a&gt;  == 关于 ==  &amp;lt;span style=&amp;quot;color: blue; font-size: 150%&amp;quot;&amp;gt;&amp;#039;&amp;#039;&amp;#039;ThreadLocal&amp;#039;&amp;#039;&amp;#039;&amp;lt;/span&amp;gt; 关键字的概念与作用，在&amp;lt;u&amp;gt;“&amp;#039;&amp;#039;&amp;#039;Java 核心技术&amp;#039;&amp;#039;&amp;#039;”（卷 I）的“&amp;#039;&amp;#039;&amp;#039;并发&amp;#039;&amp;#039;&amp;#039;”部分&amp;lt;/u&amp;gt;&amp;lt;ref name=&amp;quot;并发&amp;quot;/&amp;gt;与&amp;lt;u&amp;gt;“&amp;#039;&amp;#039;&amp;#039;深入理解 JVM 虚拟机&amp;#039;&amp;#039;&amp;#039;”的“&amp;#039;&amp;#039;&amp;#039;线程安全&amp;#039;&amp;#039;&amp;#039;”部分&amp;lt;/u&amp;gt;&amp;lt;ref name=&amp;quot;线程安全&amp;quot;/&amp;gt;已经了解差不多了。      简单来说：&amp;#039;&amp;#039;&amp;#039;ThreadLocal 就是通过定义“线程局部变量”的方式实现“线程…”&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[category:Java]]&lt;br /&gt;
&lt;br /&gt;
== 关于 ==&lt;br /&gt;
 &amp;lt;span style=&amp;quot;color: blue; font-size: 150%&amp;quot;&amp;gt;'''ThreadLocal'''&amp;lt;/span&amp;gt; 关键字的概念与作用，在&amp;lt;u&amp;gt;“'''Java 核心技术'''”（卷 I）的“'''并发'''”部分&amp;lt;/u&amp;gt;&amp;lt;ref name=&amp;quot;并发&amp;quot;/&amp;gt;与&amp;lt;u&amp;gt;“'''深入理解 JVM 虚拟机'''”的“'''线程安全'''”部分&amp;lt;/u&amp;gt;&amp;lt;ref name=&amp;quot;线程安全&amp;quot;/&amp;gt;已经了解差不多了。&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 简单来说：'''ThreadLocal 就是通过定义“线程局部变量”的方式实现“线程本地存储”'''。&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 其作用（或者说应用场景）&amp;lt;ref name=&amp;quot;作用&amp;quot;&amp;gt;&amp;lt;/ref&amp;gt;：&lt;br /&gt;
 1、&amp;lt;big&amp;gt;【&amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''变量隔离'''&amp;lt;/span&amp;gt;】&amp;lt;/big&amp;gt;&amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;为变量在每个线程中创建一个副本，'''避免同一参数在所有线程中传递'''&amp;lt;/span&amp;gt;。&lt;br /&gt;
 2、&amp;lt;big&amp;gt;【&amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''变量传递'''&amp;lt;/span&amp;gt;】&amp;lt;/big&amp;gt;&amp;lt;span style=&amp;quot;color: green&amp;quot;&amp;gt;为线程开辟独立的存储空间，用于在'''同一线程的流程中保持上下文'''&amp;lt;/span&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
== 结构关系 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 应用过程 ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== 参考 ==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref name=&amp;quot;并发&amp;quot;&amp;gt;参考：【&amp;lt;big&amp;gt;'''[[核心技术：并发#线程局部变量（ThreadLocal）]]'''&amp;lt;/big&amp;gt;】&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref name=&amp;quot;线程安全&amp;quot;&amp;gt;参考：【&amp;lt;big&amp;gt;'''[[深入理解JVM：线程安全与锁优化#无同步方案]]'''&amp;lt;/big&amp;gt;】&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref name=&amp;quot;作用&amp;quot;&amp;gt;&lt;br /&gt;
关于 ThreadLocal 的作用：&lt;br /&gt;
#【变量隔离】：就是用于实现“&amp;lt;span style=&amp;quot;color: blue&amp;quot;&amp;gt;'''线程安全'''&amp;lt;/span&amp;gt;”。&lt;br /&gt;
#【变量传递】：参考【'''[[Hibernate笔记_4：核心知识#关于“Session_管理方式：绑定到本地线程”]]'''】（将业务层获取的 Session 传递到 DAO 层）。&lt;br /&gt;
&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
</feed>