{"id":37488,"date":"2023-12-11T14:41:40","date_gmt":"2023-12-11T06:41:40","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=37488"},"modified":"2023-12-11T14:41:40","modified_gmt":"2023-12-11T06:41:40","slug":"%e8%af%b7%e8%af%b4%e4%b8%80%e4%b8%8bthreadlocal%e7%9a%84%e5%8e%9f%e7%90%86%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e8%af%b7%e8%af%b4%e4%b8%80%e4%b8%8bthreadlocal%e7%9a%84%e5%8e%9f%e7%90%86%ef%bc%9f\/","title":{"rendered":"\u8bf7\u8bf4\u4e00\u4e0bThreadLocal\u7684\u539f\u7406\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u4ece <code>Thread<\/code>\u7c7b\u6e90\u4ee3\u7801\u5165\u624b\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;public class Thread implements Runnable { &#8230;&#8230;\/\/\u4e0e\u6b64\u7ebf\u7a0b\u6709\u5173\u7684ThreadLocal\u503c\u3002\u7531ThreadLocal\u7c7b\u7ef4\u62a4ThreadLocal.ThreadLocalMap threadLocals = null;\/\/\u4e0e\u6b64\u7ebf\u7a0b\u6709\u5173\u7684InheritableThreadLocal\u503c\u3002\u7531InheritableThreadLocal\u7c7b\u7ef4\u62a4ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; &#8230;&#8230;}&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u4ece\u4e0a\u9762&lt;code&gt;Thread&lt;\/code&gt;\u7c7b \u6e90\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa&lt;code&gt;Thread&lt;\/code&gt; \u7c7b\u4e2d\u6709\u4e00\u4e2a &lt;code&gt;threadLocals&lt;\/code&gt; \u548c \u4e00\u4e2a  &lt;code&gt;inheritableThreadLocals&lt;\/code&gt; \u53d8\u91cf\uff0c\u5b83\u4eec\u90fd\u662f &lt;code&gt;ThreadLocalMap&lt;\/code&gt;  \u7c7b\u578b\u7684\u53d8\u91cf,\u6211\u4eec\u53ef\u4ee5\u628a &lt;code&gt;ThreadLocalMap&lt;\/code&gt;  \u7406\u89e3\u4e3a&lt;code&gt;ThreadLocal&lt;\/code&gt; \u7c7b\u5b9e\u73b0\u7684\u5b9a\u5236\u5316\u7684 &lt;code&gt;HashMap&lt;\/code&gt;\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8fd9\u4e24\u4e2a\u53d8\u91cf\u90fd\u662fnull\uff0c\u53ea\u6709\u5f53\u524d\u7ebf\u7a0b\u8c03\u7528 &lt;code&gt;ThreadLocal&lt;\/code&gt; \u7c7b\u7684 &lt;code&gt;set&lt;\/code&gt;\u6216&lt;code&gt;get&lt;\/code&gt;\u65b9\u6cd5\u65f6\u624d\u521b\u5efa\u5b83\u4eec\uff0c\u5b9e\u9645\u4e0a\u8c03\u7528\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u7684\u65f6\u5019\uff0c\u6211\u4eec\u8c03\u7528\u7684\u662f&lt;code&gt;ThreadLocalMap&lt;\/code&gt;\u7c7b\u5bf9\u5e94\u7684 &lt;code&gt;get()&lt;\/code&gt;\u3001&lt;code&gt;set()&lt;\/code&gt;\u65b9\u6cd5\u3002<\/p>\n<p>&lt;code&gt;ThreadLocal&lt;\/code&gt;\u7c7b\u7684&lt;code&gt;set()&lt;\/code&gt;\u65b9\u6cd5<\/p>\n<p>&lt;pre&gt;&lt;code&gt;    public void set(T value) {        Thread t = Thread.currentThread();        ThreadLocalMap map = getMap(t);        if (map != null)            map.set(this, value);        else            createMap(t, value);    }    ThreadLocalMap getMap(Thread t) {        return t.threadLocals;    }&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u8fd9\u4e9b\u5185\u5bb9\uff0c\u6211\u4eec\u8db3\u4ee5\u901a\u8fc7\u731c\u6d4b\u5f97\u51fa\u7ed3\u8bba\uff1a&lt;strong&gt;\u6700\u7ec8\u7684\u53d8\u91cf\u662f\u653e\u5728\u4e86\u5f53\u524d\u7ebf\u7a0b\u7684 &lt;code&gt;ThreadLocalMap&lt;\/code&gt; \u4e2d\uff0c\u5e76\u4e0d\u662f\u5b58\u5728 &lt;code&gt;ThreadLocal&lt;\/code&gt; \u4e0a\uff0cThreadLocal \u53ef\u4ee5\u7406\u89e3\u4e3a\u53ea\u662fThreadLocalMap\u7684\u5c01\u88c5\uff0c\u4f20\u9012\u4e86\u53d8\u91cf\u503c\u3002&lt;\/strong&gt;<\/p>\n<p>&lt;strong&gt;\u6bcf\u4e2aThread\u4e2d\u90fd\u5177\u5907\u4e00\u4e2aThreadLocalMap\uff0c\u800cThreadLocalMap\u53ef\u4ee5\u5b58\u50a8\u4ee5ThreadLocal\u4e3akey\u7684\u952e\u503c\u5bf9\u3002&lt;\/strong&gt; \u6bd4\u5982\u6211\u4eec\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u58f0\u660e\u4e86\u4e24\u4e2a &lt;code&gt;ThreadLocal&lt;\/code&gt; \u5bf9\u8c61\u7684\u8bdd\uff0c\u4f1a\u4f7f\u7528 &lt;code&gt;Thread&lt;\/code&gt;\u5185\u90e8\u90fd\u662f\u4f7f\u7528\u4ec5\u6709\u90a3\u4e2a&lt;code&gt;ThreadLocalMap&lt;\/code&gt; \u5b58\u653e\u6570\u636e\u7684\uff0c&lt;code&gt;ThreadLocalMap&lt;\/code&gt;\u7684 key \u5c31\u662f &lt;code&gt;ThreadLocal&lt;\/code&gt;\u5bf9\u8c61\uff0cvalue \u5c31\u662f &lt;code&gt;ThreadLocal&lt;\/code&gt; \u5bf9\u8c61\u8c03\u7528&lt;code&gt;set&lt;\/code&gt;\u65b9\u6cd5\u8bbe\u7f6e\u7684\u503c\u3002&lt;code&gt;ThreadLocal&lt;\/code&gt; \u662f map\u7ed3\u6784\u662f\u4e3a\u4e86\u8ba9\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5173\u8054\u591a\u4e2a &lt;code&gt;ThreadLocal&lt;\/code&gt;\u53d8\u91cf\u3002\u8fd9\u4e5f\u5c31\u89e3\u91ca\u4e86ThreadLocal\u58f0\u660e\u7684\u53d8\u91cf\u4e3a\u4ec0\u4e48\u5728\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u4e13\u5c5e\u672c\u5730\u53d8\u91cf\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;public class Thread implements Runnable { &#8230;&#8230;\/\/\u4e0e\u6b64\u7ebf\u7a0b\u6709\u5173\u7684ThreadLocal\u503c\u3002\u7531ThreadLocal\u7c7b\u7ef4\u62a4ThreadLocal.ThreadLocalMap threadLocals = null;\/\/\u4e0e\u6b64\u7ebf\u7a0b\u6709\u5173\u7684InheritableThreadLocal\u503c\u3002\u7531InheritableThreadLocal\u7c7b\u7ef4\u62a4ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; &#8230;&#8230;}&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;code&gt;ThreadLocalMap&lt;\/code&gt;\u662f&lt;code&gt;ThreadLocal&lt;\/code&gt;\u7684\u9759\u6001\u5185\u90e8\u7c7b\u3002<\/p>\n<p>&lt;img src=&quot;&quot;https:\/\/ws1.sinaimg.cn\/large\/006rNwoDgy1g2f47u9li2j30ka08cq43.jpg&quot;&quot; referrerpolicy=&quot;&quot;no-referrer&quot;&quot; alt=&quot;&quot;ThreadLocal\u5185\u90e8\u7c7b&quot;&quot; style=&quot;&quot;width: 100%;&quot;&quot;&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u4ece Thread\u7c7b\u6e90\u4ee3\u7801\u5165\u624b\u3002 &lt;pre&gt;&lt;code&gt; [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-37488","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=37488"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37488\/revisions"}],"predecessor-version":[{"id":37489,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37488\/revisions\/37489"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=37488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=37488"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=37488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}