{"id":79957,"date":"2026-03-03T20:58:06","date_gmt":"2026-03-03T12:58:06","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/79957.html"},"modified":"2026-03-03T20:58:06","modified_gmt":"2026-03-03T12:58:06","slug":"%e3%80%8cjuc%e3%80%8d%e7%ba%bf%e7%a8%8b%e9%9a%94%e7%a6%bb%e4%b8%8e%e4%b8%8d%e5%8f%af%e5%8f%98%e6%80%a7%ef%bc%9athreadlocal-%e6%ba%90%e7%a0%81%e5%89%96%e6%9e%90%e4%b8%8e-final-%e5%85%b3%e9%94%ae","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/79957.html","title":{"rendered":"\u300cJUC\u300d\u7ebf\u7a0b\u9694\u79bb\u4e0e\u4e0d\u53ef\u53d8\u6027\uff1aThreadLocal \u6e90\u7801\u5256\u6790\u4e0e Final \u5173\u952e\u5b57"},"content":{"rendered":"<h4>\u5f15\u8a00<\/h4>\n<p>\u5728\u591a\u7ebf\u7a0b\u7684\u4e16\u754c\u91cc&#xff0c;\u89e3\u51b3\u5e76\u53d1\u5b89\u5168\u95ee\u9898\u901a\u5e38\u6709\u4e24\u4e2a\u6781\u7aef\u601d\u8def&#xff1a;\u4e00\u4e2a\u662f\u201c\u5927\u5bb6\u6392\u961f\u7528\u201d&#xff0c;\u8fd9\u5c31\u662f\u52a0\u9501&#xff08;Synchronized\/ReentrantLock&#xff09;&#xff1b;\u53e6\u4e00\u4e2a\u5219\u662f\u201c\u5404\u7528\u5404\u7684\u201d\u6216\u8005\u201c\u8c01\u90fd\u522b\u60f3\u6539\u201d\u3002\u672c\u7bc7\u535a\u5ba2\u6211\u4eec\u5c06\u8df3\u51fa\u4f20\u7edf\u7684\u52a0\u9501\u601d\u7ef4&#xff0c;\u63a2\u8ba8\u5e76\u53d1\u7f16\u7a0b\u4e2d\u6781\u5176\u4f18\u96c5\u7684\u4e24\u79cd\u65e0\u9501\u54f2\u5b66&#xff1a;\u4e0d\u53ef\u53d8\u6027\u8bbe\u8ba1&#xff08;Final&#xff09;\u4e0e\u7ebf\u7a0b\u7ea7\u9694\u79bb&#xff08;ThreadLocal&#xff09;\u3002<\/p>\n<hr \/>\n<h4>\u4e00\u3001\u7834\u9664\u5171\u4eab\u7126\u8651 \u2014\u2014 \u4e0d\u53ef\u53d8\u8bbe\u8ba1\u4e0e Final \u5173\u952e\u5b57<\/h4>\n<p>\u5f88\u591a\u65f6\u5019&#xff0c;\u591a\u7ebf\u7a0b\u7684 BUG \u6765\u6e90\u4e8e\u201c\u5171\u4eab\u4e14\u53ef\u53d8\u201d\u3002\u65e2\u7136\u540c\u65f6\u4fee\u6539\u4e00\u4e2a\u4e1c\u897f\u4f1a\u51fa\u95ee\u9898&#xff0c;\u90a3\u6211\u4eec\u5e72\u8106\u628a\u5b83\u8bbe\u8ba1\u6210\u201c\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u53ef\u4fee\u6539\u201d&#xff0c;\u95ee\u9898\u81ea\u7136\u8fce\u5203\u800c\u89e3\u3002<\/p>\n<h5>&#x1f7e1; 1. \u4f20\u7edf\u65e5\u671f\u8f6c\u6362\u95ee\u9898\u4e0e\u4fdd\u62a4\u6027\u62f7\u8d1d<\/h5>\n<p>\u5728\u5e73\u65f6\u7684\u4e1a\u52a1\u4ee3\u7801\u4e2d&#xff0c;\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u5751&#xff1a;\u628a SimpleDateFormat \u5b9a\u4e49\u6210\u9759\u6001\u5168\u5c40\u53d8\u91cf&#xff0c;\u591a\u7ebf\u7a0b\u540c\u65f6\u53bb parse() \u6216 format() \u65f6&#xff0c;\u76f4\u63a5\u62a5\u9519\u6216\u8005\u62ff\u5230\u9519\u8bef\u7684\u65f6\u95f4\u3002<\/p>\n<p>\u6253\u4e2a\u751f\u6d3b\u4e2d\u7684\u6bd4\u65b9&#xff1a;SimpleDateFormat \u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u4e2a\u5171\u4eab\u7684\u201c\u65e5\u5386\u8349\u7a3f\u672c\u201d&#xff08;Calendar \u5bf9\u8c61&#xff09;\u3002\u591a\u7ebf\u7a0b\u5e76\u53d1\u64cd\u4f5c\u65f6&#xff0c;\u76f8\u5f53\u4e8e\u597d\u51e0\u4e2a\u4eba\u540c\u65f6\u5728\u4e00\u9875\u8349\u7a3f\u672c\u4e0a\u64e6\u9664\u3001\u91cd\u5199&#xff0c;\u6700\u540e\u5ff5\u51fa\u6765\u7684\u7ed3\u679c\u80af\u5b9a\u4e71\u5957\u3002<\/p>\n<p>Java<\/p>\n<p>\/\/ \u274c \u9519\u8bef\u793a\u8303&#xff1a;\u591a\u7ebf\u7a0b\u5171\u4eab\u4e14\u53ef\u53d8&#xff0c;\u5fc5\u5b9a\u7ebf\u7a0b\u4e0d\u5b89\u5168<br \/>\npublic static final SimpleDateFormat sdf &#061; new SimpleDateFormat(&#034;yyyy-MM-dd&#034;);<\/p>\n<p>\u5982\u4f55\u89e3\u51b3&#xff1f;\u601d\u8def\u8f6c\u5411\u201c\u4e0d\u53ef\u53d8\u7c7b\u201d<\/p>\n<p>\u50cf String\u3001Integer \u4ee5\u53ca JDK 8 \u5f15\u5165\u7684 DateTimeFormatter&#xff0c;\u5929\u751f\u5c31\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5b83\u4eec\u7684\u8bbe\u8ba1\u601d\u60f3\u975e\u5e38\u4e00\u81f4&#xff1a;<\/p>\n<li>\u7c7b\u548c\u5c5e\u6027\u90fd\u52a0 final&#xff0c;\u4e0d\u5141\u8bb8\u7ee7\u627f\u548c\u91cd\u5199\u3002<\/li>\n<li>\u4e0d\u63d0\u4f9b\u4efb\u4f55 set() \u65b9\u6cd5\u4fee\u6539\u5185\u90e8\u72b6\u6001\u3002<\/li>\n<li>\u4fdd\u62a4\u6027\u62f7\u8d1d&#xff1a;\u5f53\u4f60\u60f3\u8c03\u7528 replace() \u4fee\u6539\u5b57\u7b26\u4e32\u65f6&#xff0c;\u5b83\u4e0d\u662f\u5728\u539f\u5bf9\u8c61\u4e0a\u6539&#xff0c;\u800c\u662f new \u4e00\u4e2a\u5168\u65b0\u7684\u5b57\u7b26\u4e32\u8fd4\u56de\u7ed9\u4f60\u3002\u5927\u5bb6\u53ea\u8bfb\u4e0d\u5199&#xff0c;\u81ea\u7136\u6ca1\u6709\u5e76\u53d1\u51b2\u7a81\u3002<\/li>\n<hr \/>\n<h5>&#x1f534; 2. \u6df1\u5165\u5e95\u5c42&#xff1a;Final \u5173\u952e\u5b57\u7684\u5185\u5b58\u8bed\u4e49<\/h5>\n<p>\u53ea\u77e5\u9053 final \u53d8\u91cf\u4e0d\u80fd\u88ab\u4fee\u6539\u662f\u8fdc\u8fdc\u4e0d\u591f\u7684\u3002\u9762\u8bd5\u5b98\u5f80\u5f80\u4f1a\u5f80\u4e0b\u6316\u4e00\u5c42&#xff1a;\u5728\u5e76\u53d1\u73af\u5883\u4e0b&#xff0c;**final**** \u662f\u600e\u4e48\u4fdd\u8bc1\u5b89\u5168\u7684&#xff1f;**<\/p>\n<p>\u8fd9\u5c31\u8981\u4ece\u6307\u4ee4\u91cd\u6392\u5e8f\u8bf4\u8d77\u3002\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u901a\u5e38\u9700\u8981\u4e09\u6b65&#xff1a;\u5206\u914d\u5185\u5b58\u7a7a\u95f4\u3001\u521d\u59cb\u5316\u5bf9\u8c61\u3001\u5c06\u5185\u5b58\u5730\u5740\u8d4b\u503c\u7ed9\u5f15\u7528\u3002\u5982\u679c\u53d1\u751f\u6307\u4ee4\u91cd\u6392&#xff0c;\u7ebf\u7a0b B \u53ef\u80fd\u4f1a\u62ff\u5230\u4e00\u4e2a\u5206\u914d\u4e86\u5730\u5740\u4f46\u8fd8\u6ca1\u521d\u59cb\u5316\u5b8c\u6210\u7684\u201c\u534a\u6210\u54c1\u201d\u5bf9\u8c61&#xff0c;\u8bfb\u53d6\u5230\u91cc\u9762\u5c5e\u6027\u7684\u9ed8\u8ba4\u96f6\u503c&#xff08;\u5982 0 \u6216 null&#xff09;\u3002<\/p>\n<p>\u800c final \u5173\u952e\u5b57&#xff0c;\u5c31\u662f JVM \u5c42\u9762\u7ed9\u51fa\u7684\u5f3a\u6709\u529b\u627f\u8bfa\u3002<\/p>\n<p>\u6838\u5fc3\u63a8\u6f14&#xff1a;<\/p>\n<li>\u5b57\u8282\u7801\u5c42\u9762&#xff1a;\u7ed9 final \u53d8\u91cf\u8d4b\u503c\u7684\u64cd\u4f5c&#xff0c;\u5728\u5b57\u8282\u7801\u4e2d\u5bf9\u5e94\u7740 putfield \u6307\u4ee4\u3002<\/li>\n<li>\u5185\u5b58\u5c4f\u969c&#xff08;\u6838\u5fc3&#xff09;&#xff1a;\u7f16\u8bd1\u5668\u4f1a\u5728 final \u53d8\u91cf\u7684\u5199\u64cd\u4f5c&#xff08;putfield&#xff09;\u4e4b\u540e\u3001\u6784\u9020\u51fd\u6570\u8fd4\u56de\u4e4b\u524d&#xff0c;\u63d2\u5165\u4e00\u4e2a StoreStore \u5185\u5b58\u5c4f\u969c&#xff08;\u5199\u5c4f\u969c&#xff09;\u3002<\/li>\n<li>\u53ef\u89c1\u6027\u4fdd\u969c&#xff1a;\u8fd9\u4e2a\u5c4f\u969c\u5f3a\u5236\u8981\u6c42&#xff1a;final \u53d8\u91cf\u7684\u8d4b\u503c\u5fc5\u987b\u5728\u5bf9\u8c61\u5f15\u7528\u88ab\u5176\u4ed6\u7ebf\u7a0b\u770b\u5230\u4e4b\u524d\u5b8c\u6210\u3002\u8fd9\u5c31\u7edd\u5bf9\u7981\u6b62\u4e86\u201c\u5148\u628a\u5bf9\u8c61\u5f15\u7528\u66b4\u9732\u51fa\u53bb&#xff0c;\u518d\u6162\u6162\u521d\u59cb\u5316 final \u53d8\u91cf\u201d\u7684\u91cd\u6392\u5e8f\u884c\u4e3a\u3002\u56e0\u6b64&#xff0c;\u53ea\u8981\u5176\u4ed6\u7ebf\u7a0b\u62ff\u5230\u4e86\u8fd9\u4e2a\u5bf9\u8c61&#xff0c;\u5b83\u8bfb\u5230\u7684 final \u5c5e\u6027\u5c31\u4e00\u5b9a\u662f\u521d\u59cb\u5316\u597d\u7684\u6b63\u786e\u503c&#xff0c;\u7edd\u4e0d\u4f1a\u662f\u96f6\u503c\u3002<\/li>\n<p>&#x1f4a1;** \u7ed3\u5408\u771f\u5b9e\u4e1a\u52a1\u9632\u5751\u573a\u666f**<\/p>\n<p>\u9762\u8bd5\u5b98&#xff1a;\u5728\u54b1\u4eec\u5916\u5356\u7cfb\u7edf\u7684\u5546\u54c1\u8be6\u60c5\u9875&#xff0c;\u5982\u679c\u6211\u7528 DCL&#xff08;\u53cc\u91cd\u68c0\u67e5\u9501&#xff09;\u5199\u4e86\u4e00\u4e2a\u672c\u5730\u7f13\u5b58\u7684\u5355\u4f8b&#xff0c;\u4f46\u662f\u5fd8\u8bb0\u52a0 volatile \u4fee\u9970\u5b9e\u4f8b\u5f15\u7528\u4e86&#xff0c;\u800c\u5355\u4f8b\u5185\u90e8\u7684\u5c5e\u6027\u5168\u662f final \u7684\u3002\u8bf7\u95ee\u9ad8\u5e76\u53d1\u4e0b\u4f1a\u6709\u5e76\u53d1\u95ee\u9898\u5417&#xff1f;<\/p>\n<p>\u7834\u5c40\u56de\u7b54&#xff1a;\u4e0d\u4f1a\u51fa\u73b0\u5c5e\u6027\u672a\u521d\u59cb\u5316\u7684\u5e76\u53d1\u95ee\u9898\u3002\u867d\u7136\u5355\u4f8b\u5f15\u7528\u6ca1\u6709\u52a0 volatile&#xff0c;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5f15\u7528\u672c\u8eab\u7684\u8d4b\u503c\u53d1\u751f\u91cd\u6392\u5e8f&#xff0c;\u4f46\u56e0\u4e3a\u5185\u90e8\u5c5e\u6027\u662f final \u7684&#xff0c;JSR-133&#xff08;Java \u5185\u5b58\u6a21\u578b\u89c4\u8303&#xff09;\u589e\u5f3a\u4e86 final \u7684\u8bed\u4e49\u3002final \u7684\u5199\u5c4f\u969c\u4fdd\u8bc1\u4e86\u5728\u6784\u9020\u51fd\u6570\u7ed3\u675f\u524d&#xff0c;final \u5c5e\u6027\u5fc5\u5b9a\u521d\u59cb\u5316\u5b8c\u6bd5\u3002\u6240\u4ee5&#xff0c;\u53ea\u8981\u7ebf\u7a0b\u80fd\u62ff\u5230\u8fd9\u4e2a\u5355\u4f8b\u5bf9\u8c61&#xff0c;\u8bfb\u5230\u7684 final \u5c5e\u6027\u5fc5\u5b9a\u662f\u6b63\u786e\u6784\u5efa\u7684\u3002\u4e0d\u8fc7&#xff0c;\u4e3a\u4e86\u7a0b\u5e8f\u7684\u7edd\u5bf9\u4e25\u8c28&#xff0c;DCL \u5355\u4f8b\u7684\u5f15\u7528\u8fd8\u662f\u5f3a\u70c8\u5efa\u8bae\u52a0\u4e0a volatile\u3002<\/p>\n<hr \/>\n<h5>&#x1f7e1; 3. \u4eab\u5143\u6a21\u5f0f\u4e0e\u65e0\u9501\u8fde\u63a5\u6c60<\/h5>\n<h6>3.1 \u4ec0\u4e48\u662f\u4eab\u5143\u6a21\u5f0f<\/h6>\n<p>\u5728\u6ca1\u6709\u4eab\u5143\u6a21\u5f0f\u4e4b\u524d&#xff0c;\u7cfb\u7edf\u6bcf\u9700\u8981\u64cd\u4f5c\u4e00\u6b21\u6570\u636e\u5e93&#xff0c;\u5c31 new Connection() \u521b\u5efa\u4e00\u4e2a\u8fde\u63a5&#xff0c;\u7528\u5b8c\u5c31\u9500\u6bc1\u3002\u4f46\u521b\u5efa\u8fde\u63a5\u6781\u5176\u8017\u65f6&#xff08;\u8981\u8fdb\u884c TCP \u4e09\u6b21\u63e1\u624b\u7b49&#xff09;\u3002<\/p>\n<p>\u4eab\u5143\u6a21\u5f0f\u7684\u6838\u5fc3\u601d\u60f3\u5c31\u662f&#xff1a;\u5bf9\u8c61\u590d\u7528&#xff08;\u63d0\u524d\u5efa\u597d&#xff0c;\u501f\u6765\u501f\u53bb&#xff09;\u3002 \u5bf9\u5e94\u5230\u4ee3\u7801\u91cc&#xff1a;<\/p>\n<li>connections \u6570\u7ec4&#xff1a;\u76f8\u5f53\u4e8e\u4e00\u4e2a\u5171\u4eab\u5145\u7535\u5b9d\u673a\u67dc\u3002\u6211\u4eec\u5728 new MockConnectionPool(10) \u7684\u65f6\u5019&#xff0c;\u5c31\u63d0\u524d\u4e70\u597d\u4e86 10 \u4e2a\u5145\u7535\u5b9d&#xff08;\u8fde\u63a5\u5bf9\u8c61&#xff09;\u585e\u8fdb\u6570\u7ec4\u91cc\u3002\u4ee5\u540e\u8c01\u4e5f\u4e0d\u8bb8\u518d new \u4e86&#xff0c;\u53ea\u80fd\u4ece\u8fd9\u91cc\u9762\u501f\u3002<\/li>\n<li>states \u6570\u7ec4&#xff1a;\u8fd9\u662f\u673a\u67dc\u4e0a\u7684\u6307\u793a\u706f\u3002\u5b83\u548c\u5145\u7535\u5b9d\u4e00\u4e00\u5bf9\u5e94\u30020 \u4ee3\u8868\u7eff\u706f&#xff08;\u7a7a\u95f2\u53ef\u501f&#xff09;&#xff0c;1 \u4ee3\u8868\u7ea2\u706f&#xff08;\u88ab\u4eba\u501f\u8d70\u7e41\u5fd9&#xff09;\u3002<\/li>\n<h6>3.2 \u4e3a\u4ec0\u4e48\u53eb\u201c\u65e0\u9501\u201d<\/h6>\n<p>\u65e2\u7136\u662f\u5171\u4eab\u5145\u7535\u5b9d&#xff0c;\u5047\u8bbe\u73b0\u5728\u6709\u4e24\u4e2a\u7ebf\u7a0b&#xff08;\u5f20\u4e09\u548c\u674e\u56db&#xff09;\u540c\u65f6\u6765\u501f\u5145\u7535\u5b9d\u3002\u5982\u679c\u6211\u4eec\u7528 synchronized \u52a0\u9501&#xff0c;\u90a3\u5c31\u662f\u5f20\u4e09\u5148\u501f&#xff0c;\u674e\u56db\u5728\u540e\u9762\u6392\u961f\u5e72\u7b49&#xff08;\u963b\u585e&#xff09;\u3002<\/p>\n<p>\u600e\u4e48\u505a\u5230\u4e0d\u6392\u961f&#xff08;\u65e0\u9501&#xff09;\u5462&#xff1f;\u9760\u7684\u662f **borrow()** \u65b9\u6cd5\u91cc\u7684\u81ea\u65cb &#043; CAS\u3002<\/p>\n<p>\u6211\u4eec\u6765\u770b\u5f20\u4e09\u501f\u5145\u7535\u5b9d\u7684\u52a8\u4f5c\u62c6\u89e3&#xff08;\u770b borrow \u65b9\u6cd5&#xff09;&#xff1a;<\/p>\n<li>**while(true)**&#xff08;\u81ea\u65cb&#xff09;&#xff1a;\u5f20\u4e09\u662f\u4e2a\u6025\u6027\u5b50&#xff0c;\u4ed6\u4e0d\u7761\u89c9\u4e0d\u6392\u961f&#xff0c;\u501f\u4e0d\u5230\u5c31\u4e00\u76f4\u5728\u673a\u67dc\u524d\u75af\u72c2\u626b\u89c6&#xff08;\u6b7b\u5faa\u73af&#xff09;\u3002<\/li>\n<li>**if (states.get(i) &#061;&#061; 0)**&#xff1a;\u5f20\u4e09\u987a\u7740\u673a\u67dc\u770b&#xff0c;\u53d1\u73b0\u7b2c 3 \u53f7\u5145\u7535\u5b9d\u6307\u793a\u706f\u662f 0&#xff08;\u7eff\u706f&#xff09;\u3002<\/li>\n<li>**states.compareAndSet(i, 0, 1)**&#xff08;CAS \u62a2\u5360&#xff09;&#xff1a;\u8fd9\u662f\u6700\u6838\u5fc3\u7684\u4e00\u6b65&#xff01;\n<ul>\n<li>\u5047\u8bbe\u5f20\u4e09\u548c\u674e\u56db\u540c\u65f6\u770b\u5230\u4e86 3 \u53f7\u5145\u7535\u5b9d\u662f\u7eff\u706f&#xff08;0&#xff09;\u3002<\/li>\n<li>\u4e24\u4eba\u540c\u65f6\u4f38\u624b\u53bb\u6309\u6309\u94ae&#xff0c;\u60f3\u628a\u706f\u53d8\u6210\u7ea2\u706f&#xff08;1&#xff09;\u3002<\/li>\n<li>CAS&#xff08;Compare-And-Swap&#xff09;\u662f CPU \u786c\u4ef6\u7ea7\u522b\u4fdd\u8bc1\u7684\u539f\u5b50\u64cd\u4f5c\u3002\u786c\u4ef6\u89c4\u5b9a&#xff1a;\u65e0\u8bba\u591a\u5c11\u4eba\u540c\u65f6\u6309&#xff0c;\u7edd\u5bf9\u53ea\u6709\u4e00\u4e2a\u4eba\u80fd\u6309\u6210\u529f&#xff01;<\/li>\n<li>\u5047\u8bbe\u5f20\u4e09\u6309\u6210\u529f\u4e86&#xff0c;compareAndSet \u8fd4\u56de true&#xff0c;\u5f20\u4e09\u9ad8\u5174\u5730\u628a 3 \u53f7\u8fde\u63a5\u62ff\u8d70 return connections[i];\u3002<\/li>\n<li>\u674e\u56db\u624b\u6162\u4e860.001\u79d2&#xff0c;\u4ed6\u6309\u7684\u65f6\u5019\u53d1\u73b0\u706f\u5df2\u7ecf\u53d8\u6210 1 \u4e86&#xff0c;CAS \u5931\u8d25\u8fd4\u56de false\u3002<\/li>\n<li>\u674e\u56db\u600e\u4e48\u529e&#xff1f;\u6ca1\u4e8b&#xff0c;\u4ed6\u5728 while(true) \u5faa\u73af\u91cc&#xff0c;\u7acb\u523b\u518d\u53bb\u5bfb\u627e\u4e0b\u4e00\u4e2a 0 \u7684\u5145\u7535\u5b9d\u3002\u6574\u4e2a\u8fc7\u7a0b\u674e\u56db\u6ca1\u6709\u88ab\u6302\u8d77&#xff0c;\u53ea\u662f\u5728\u7a7a\u8f6c&#xff08;\u6d88\u8017\u4e00\u70b9 CPU&#xff09;&#xff0c;\u901f\u5ea6\u6781\u5feb\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u4e0b\u9762\u6211\u7ed3\u5408 CAS \u548c\u539f\u5b50\u6570\u7ec4 AtomicIntegerArray&#xff0c;\u624b\u5199\u4e00\u4e2a\u6781\u7b80\u7684\u65e0\u9501\u6570\u636e\u5e93\u8fde\u63a5\u6c60&#xff0c;\u4f53\u4f1a\u4e00\u4e0b\u5982\u4f55\u65e0\u9501\u5316\u5730\u7ba1\u7406\u5171\u4eab\u8d44\u6e90&#xff1a;<\/p>\n<p>Java<\/p>\n<p>class MockConnectionPool {<br \/>\n    private final Connection[] connections;<br \/>\n    \/\/ \u72b6\u6001\u6570\u7ec4&#xff1a;0\u8868\u793a\u7a7a\u95f2&#xff0c;1\u8868\u793a\u7e41\u5fd9\u3002\u4f7f\u7528\u539f\u5b50\u6570\u7ec4\u4fdd\u8bc1\u5e76\u53d1\u5b89\u5168<br \/>\n    private final AtomicIntegerArray states; <\/p>\n<p>    public MockConnectionPool(int poolSize) {<br \/>\n        connections &#061; new Connection[poolSize];<br \/>\n        states &#061; new AtomicIntegerArray(poolSize); \/\/ \u9ed8\u8ba4\u5168\u4e3a0<br \/>\n        for (int i &#061; 0; i &lt; poolSize; i&#043;&#043;) {<br \/>\n            connections[i] &#061; new MockConnection(); \/\/ \u63d0\u524d\u521b\u5efa\u597d\u5bf9\u8c61&#xff08;\u4eab\u5143&#xff09;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u501f\u51fa\u8fde\u63a5<br \/>\n    public Connection borrow() {<br \/>\n        while (true) { \/\/ \u81ea\u65cb\u5c1d\u8bd5<br \/>\n            for (int i &#061; 0; i &lt; states.length(); i&#043;&#043;) {<br \/>\n                if (states.get(i) &#061;&#061; 0) {<br \/>\n                    \/\/ CAS\u5c1d\u8bd5\u5c06\u72b6\u6001\u4ece0\u6539\u4e3a1&#xff0c;\u6210\u529f\u5219\u4ee3\u8868\u62ff\u5230\u4e86\u8fde\u63a5<br \/>\n                    if (states.compareAndSet(i, 0, 1)) {<br \/>\n                        return connections[i];<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n            \/\/ \u515c\u5e95&#xff1a;\u5982\u679c\u6ca1\u6709\u7a7a\u95f2\u8fde\u63a5&#xff0c;\u53ef\u4ee5\u9009\u62e9\u4f11\u7720\/\u963b\u585e\u7b49\u5f85&#8230;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u5f52\u8fd8\u8fde\u63a5<br \/>\n    public void free(Connection conn) {<br \/>\n        for (int i &#061; 0; i &lt; connections.length; i&#043;&#043;) {<br \/>\n            if (connections[i] &#061;&#061; conn) {<br \/>\n                states.set(i, 0); \/\/ \u5f52\u8fd8&#xff0c;\u56e0\u4e3a\u662f\u88ab\u5f53\u524d\u7ebf\u7a0b\u72ec\u5360&#xff0c;\u6240\u4ee5\u76f4\u63a5set\u4e3a0\u5373\u53ef<br \/>\n                break;<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f&#xff0c;\u6211\u4eec\u4e0d\u4ec5\u590d\u7528\u4e86\u5bf9\u8c61&#xff0c;\u8fd8\u901a\u8fc7 CAS \u64cd\u4f5c\u5b8c\u7f8e\u907f\u5f00\u4e86\u91cd\u91cf\u7ea7\u7684\u963b\u585e\u9501&#xff0c;\u5927\u5e45\u63d0\u5347\u4e86\u5e76\u53d1\u6027\u80fd\u3002\u4e4b\u540e\u6211\u4eec\u5c06\u4ecb\u7ecd\u53e6\u4e00\u79cd\u66f4\u52a0\u5f7b\u5e95\u7684\u65e0\u9501\u601d\u8def&#xff08;ThreadLocal&#xff09;\u2014\u2014\u65e2\u7136\u5171\u4eab\u8fd9\u4e48\u9ebb\u70e6&#xff0c;\u5e72\u8106\u5c31\u4e0d\u5171\u4eab\u4e86&#xff01;<\/p>\n<h4>\u4e8c\u3001\u5404\u5bb6\u81ea\u626b\u95e8\u524d\u96ea &#8211; ThreadLocal \u6838\u5fc3\u5e94\u7528\u4e0e\u67b6\u6784\u6f14\u8fdb<\/h4>\n<h5>&#x1f7e2; 1. \u4e00\u53e5\u8bdd\u770b\u900f ThreadLocal vs Synchronized<\/h5>\n<p>\u5982\u679c\u7528\u4e00\u53e5\u8bdd\u6765\u603b\u7ed3\u5b83\u4eec\u7684\u672c\u8d28\u533a\u522b&#xff1a; Synchronized \u662f\u201c\u4ee5\u65f6\u95f4\u6362\u7a7a\u95f4\u201d&#xff08;\u5927\u5bb6\u6392\u961f\u53bb\u62ff\u540c\u4e00\u4e2a\u6587\u5177\u76d2\u91cc\u7684\u7b14&#xff0c;\u6162\u4f46\u7701\u7b14&#xff09;&#xff0c;\u800c ThreadLocal \u662f\u201c\u4ee5\u7a7a\u95f4\u6362\u65f6\u95f4\u201d&#xff08;\u6211\u7ed9\u6bcf\u4e2a\u4eba\u53e3\u888b\u91cc\u90fd\u585e\u4e00\u652f\u4e13\u5c5e\u7684\u7b14&#xff0c;\u4e0d\u7528\u62a2&#xff0c;\u5feb\u4f46\u8d39\u7b14&#xff09;\u3002<\/p>\n<p>ThreadLocal \u538b\u6839\u4e0d\u662f\u4e3a\u4e86\u89e3\u51b3\u201c\u591a\u7ebf\u7a0b\u5171\u4eab\u53d8\u91cf\u201d\u7684\u5e76\u53d1\u95ee\u9898&#xff0c;\u800c\u662f\u4e3a\u4e86\u5b9e\u73b0\u7ebf\u7a0b\u7ea7\u7684\u6570\u636e\u9694\u79bb\u3002<\/p>\n<hr \/>\n<h5>&#x1f7e1; 2. \u65e5\u5e38\u642c\u7816\u4e09\u677f\u65a7&#xff1a;\u6838\u5fc3 API \u4e0e\u7ecf\u5178\u573a\u666f<\/h5>\n<p>\u5728\u5b9e\u9645\u4e1a\u52a1\u5f00\u53d1\u4e2d&#xff0c;ThreadLocal \u7684 API \u975e\u5e38\u5e72\u51c0\u5229\u843d\u3002\u6211\u4eec\u5c31\u56db\u4e2a\u5e38\u7528\u65b9\u6cd5&#xff1a;set()\u3001get()\u3001remove() \u548c\u91cd\u5199\u521d\u59cb\u503c\u7684 initialValue()\u3002<\/p>\n<p>\u65e5\u5e38\u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u901a\u5e38\u62ff\u5b83\u5e72\u8fd9\u4e09\u4ef6\u4e8b&#xff1a;<\/p>\n<li>\u89e3\u51b3\u975e\u7ebf\u7a0b\u5b89\u5168\u5bf9\u8c61\u7684\u5e76\u53d1\u95ee\u9898&#xff1a;\u6700\u7ecf\u5178\u7684\u5c31\u662f\u5f7b\u5e95\u5e72\u6389 SimpleDateFormat \u7684\u5e76\u53d1 BUG&#xff0c;\u7ed9\u6bcf\u4e2a\u7ebf\u7a0b\u53d1\u4e00\u4efd\u4e13\u5c5e\u7684\u683c\u5f0f\u5316\u5de5\u5177\u3002<\/li>\n<li>\u8de8\u5c42\u7ea7\u7684\u6570\u636e\u900f\u4f20&#xff08;Session\/TraceId&#xff09;&#xff1a;\u5728 Web \u5f00\u53d1\u4e2d&#xff0c;\u62e6\u622a\u5668\u89e3\u6790\u51fa\u7528\u6237\u7684 Session \u6216\u94fe\u8def\u8ffd\u8e2a\u7684 TraceId \u540e&#xff0c;\u585e\u8fdb ThreadLocal&#xff0c;\u540e\u9762\u7684 Service\/Dao \u5c42\u76f4\u63a5 get() \u5c31\u80fd\u62ff&#xff0c;\u514d\u53bb\u4e86\u5728\u65b9\u6cd5\u53c2\u6570\u91cc\u4f20\u6765\u4f20\u53bb\u7684\u6076\u5fc3\u4ee3\u7801\u3002<\/li>\n<li>\u6570\u636e\u5e93\u4e8b\u52a1\u9694\u79bb&#xff1a;Spring \u7684 &#064;Transactional \u5e95\u5c42\u5c31\u662f\u628a\u62ff\u5230\u7684 Connection \u7ed1\u5b9a\u5230\u4e86\u5f53\u524d\u7ebf\u7a0b\u7684 ThreadLocal \u91cc&#xff0c;\u4fdd\u8bc1\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u7684\u6240\u6709 DAO \u64cd\u4f5c\u7528\u7684\u90fd\u662f\u540c\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/li>\n<p>\u6781\u7b80\u4ee3\u7801\u793a\u8303&#xff1a;<\/p>\n<p>Java<\/p>\n<p>public class ThreadLocalDemo {<br \/>\n    \/\/ \u8fd9\u5c31\u662f\u4e00\u4e2a\u201c\u9b54\u6cd5\u50a8\u7269\u67dc\u201d<br \/>\n    static ThreadLocal&lt;String&gt; tl &#061; new ThreadLocal&lt;&gt;();<\/p>\n<p>    public static void main(String[] args) {<\/p>\n<p>        \/\/ \u5f20\u4e09\u7ebf\u7a0b<br \/>\n        new Thread(() -&gt; {<br \/>\n            tl.set(&#034;\u82f9\u679c&#034;); \/\/ \u5f20\u4e09\u5f80\u67dc\u5b50\u91cc\u653e\u4e86\u82f9\u679c<br \/>\n            try { Thread.sleep(2000); } catch (Exception e) {} \/\/ \u7761\u4e00\u4f1a&#xff0c;\u7b49\u674e\u56db\u653e\u5b8c<br \/>\n            System.out.println(&#034;\u5f20\u4e09\u62ff\u5230\u4e86: &#034; &#043; tl.get()); \/\/ \u6253\u5370&#xff1a;\u5f20\u4e09\u62ff\u5230\u4e86: \u82f9\u679c<br \/>\n        }, &#034;\u5f20\u4e09&#034;).start();<\/p>\n<p>        \/\/ \u674e\u56db\u7ebf\u7a0b<br \/>\n        new Thread(() -&gt; {<br \/>\n            tl.set(&#034;\u9999\u8549&#034;); \/\/ \u674e\u56db\u5f80\u540c\u4e00\u53f0\u67dc\u5b50\u91cc\u653e\u4e86\u9999\u8549<br \/>\n            System.out.println(&#034;\u674e\u56db\u62ff\u5230\u4e86: &#034; &#043; tl.get()); \/\/ \u6253\u5370&#xff1a;\u674e\u56db\u62ff\u5230\u4e86: \u9999\u8549<br \/>\n        }, &#034;\u674e\u56db&#034;).start();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u4e3a\u4ec0\u4e48\u8bf4\u5b83\u662f\u5404\u5bb6\u81ea\u626b\u95e8\u524d\u96ea&#xff1f;<\/p>\n<p>\u56e0\u4e3a _ThreadLocal_ \u5176\u5b9e\u662f\u4e2a\u201c\u76ae\u5305\u516c\u53f8\u201d\u3002\u5f53\u4f60\u8c03\u7528 _tl.set(&#034;\u82f9\u679c&#034;)_ \u7684\u65f6\u5019&#xff0c;_ThreadLocal__ \u5e76\u6ca1\u6709\u628a\u201c\u82f9\u679c\u201d\u5b58\u5728\u81ea\u5df1\u809a\u5b50\u91cc&#xff0c;\u800c\u662f_\u6084\u6084\u6252\u5f00\u4e86\u5f53\u524d\u7ebf\u7a0b&#xff08;\u5f20\u4e09&#xff09;\u7684\u53e3\u888b&#xff0c;\u628a\u201c\u82f9\u679c\u201d\u585e\u8fdb\u4e86\u5f20\u4e09\u81ea\u5df1\u7684\u53e3\u888b\u91cc\u3002<\/p>\n<hr \/>\n<h5>&#x1f534; 3. \u67b6\u6784\u6f14\u8fdb&#xff1a;\u4e3a\u4ec0\u4e48 Doug Lea \u8981\u201c\u4e7e\u5764\u5927\u632a\u79fb\u201d&#xff1f;<\/h5>\n<p>\u5728 JDK 8 \u524d\u540e&#xff0c;ThreadLocal \u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u53d1\u751f\u4e86\u4e00\u6b21\u6781\u5176\u5173\u952e\u7684\u201c\u4e7e\u5764\u5927\u632a\u79fb\u201d&#xff0c;\u8fd9\u662f\u5927\u5382\u9762\u8bd5\u7684\u8d85\u7ea7\u9ad8\u9891\u8003\u70b9\u3002<\/p>\n<ul>\n<li>\u65e9\u671f\u8bbe\u8ba1&#xff08;JDK 8 \u4e4b\u524d&#xff09;&#xff1a;ThreadLocal \u81ea\u8eab\u7ef4\u62a4\u4e00\u4e2a\u7c7b\u4f3c Map \u7684\u7ed3\u6784\u3002\u6bcf\u6765\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u5c31\u628a Thread \u5f53\u4f5c Key&#xff0c;\u5b58\u5165\u7684\u6570\u636e\u5f53\u4f5c Value\u3002<\/li>\n<li>\u73b0\u4ee3\u8bbe\u8ba1&#xff08;JDK 8 \u4e4b\u540e&#xff09;&#xff1a; \u53cd\u8fc7\u6765\u4e86&#xff01;\u662f\u6bcf\u4e2a Thread \u5bf9\u8c61\u5185\u90e8\u7ef4\u62a4\u4e86\u4e00\u4e2a ThreadLocalMap\u3002\u6bcf\u585e\u5165\u4e00\u4e2a\u6570\u636e&#xff0c;\u5c31\u628a ThreadLocal \u5b9e\u4f8b\u672c\u8eab\u5f53\u4f5c Key&#xff0c;\u5b58\u5165\u7684\u6570\u636e\u5f53\u4f5c Value\u3002<\/li>\n<\/ul>\n<p>&#x1f4a1;****JDK 8 \u4e3a\u4ec0\u4e48\u8981\u53cd\u8f6c ThreadLocalMap \u7684\u8bbe\u8ba1&#xff1f;<\/p>\n<p>\u9762\u8bd5\u5b98&#xff1a;\u6211\u770b\u4f60\u7b80\u5386\u4e0a\u5199\u4e86\u719f\u6089\u5e76\u53d1\u7f16\u7a0b\u3002\u90a3\u4f60\u8bf4\u8bf4&#xff0c;\u4e3a\u4ec0\u4e48\u73b0\u5728\u7684 Java \u7248\u672c&#xff0c;\u8981\u628a ThreadLocalMap \u6302\u5728 Thread \u8eab\u4e0a&#xff0c;\u800c\u4e0d\u662f\u50cf\u4ee5\u524d\u90a3\u6837\u6302\u5728 ThreadLocal \u8eab\u4e0a&#xff1f;<\/p>\n<p>\u56de\u7b54&#xff1a; Doug Lea \u5927\u795e\u8fd9\u6837\u6539&#xff0c;\u6838\u5fc3\u662f\u4e3a\u4e86\u89e3\u51b3\u6027\u80fd\u548c\u5185\u5b58\u751f\u547d\u5468\u671f\u4e24\u5927\u75db\u70b9&#xff1a;<\/p>\n<li>\u5927\u5e45\u51cf\u5c11 Hash \u51b2\u7a81&#xff08;\u6027\u80fd\u4f18\u5316&#xff09;&#xff1a; \u5728\u5b9e\u9645\u7684\u9ad8\u5e76\u53d1\u4e1a\u52a1&#xff08;\u6bd4\u5982\u5916\u5356\u79d2\u6740\u7cfb\u7edf&#xff09;\u4e2d&#xff0c;\u7ebf\u7a0b\u6570\u91cf\u5f80\u5f80\u662f\u975e\u5e38\u5e9e\u5927\u7684&#xff08;\u6bd4\u5982 Tomcat \u7ebf\u7a0b\u6c60\u5e38\u9a7b 200 \u4e2a\u7ebf\u7a0b&#xff09;&#xff0c;\u800c\u5b9a\u4e49\u51fa\u6765\u7684 ThreadLocal \u5bf9\u8c61\u901a\u5e38\u53ea\u6709\u90a3\u4e48\u51e0\u4e2a&#xff08;\u6bd4\u5982\u5b58\u7528\u6237\u4fe1\u606f\u7684\u3001\u5b58\u4e8b\u52a1\u8fde\u63a5\u7684&#xff09;\u3002 \u5982\u679c\u7528\u8001\u8bbe\u8ba1&#xff0c;Map \u91cc\u9762\u4f1a\u585e\u6ee1\u4e0a\u767e\u4e2a Entry&#xff08;\u952e\u503c\u5bf9&#xff09;&#xff0c;Hash \u51b2\u7a81\u6781\u5176\u4e25\u91cd&#xff1b;\u800c\u65b0\u8bbe\u8ba1\u4e0b&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u81ea\u5df1\u7684 Map \u91cc\u53ea\u6709\u5c48\u6307\u53ef\u6570\u7684\u51e0\u4e2a Entry&#xff0c;\u67e5\u627e\u6548\u7387\u9ad8\u5f97\u98de\u8d77\u3002<\/li>\n<li>\u5929\u751f\u7684\u5185\u5b58\u56de\u6536\u751f\u547d\u5468\u671f\u7ed1\u5b9a&#xff08;\u9632 OOM&#xff09;&#xff1a; \u8fd9\u662f\u6700\u81f4\u547d\u7684\u4e00\u70b9\u3002\u5982\u679c Map \u7531 ThreadLocal \u7ef4\u62a4&#xff0c;\u90a3\u53ea\u8981 ThreadLocal \u4e0d\u6b7b&#xff0c;\u91cc\u9762\u6240\u6709\u7ebf\u7a0b\u7684\u6570\u636e\u90fd\u4f1a\u4e00\u76f4\u6d3b\u7740\u3002\u4f46\u5728\u73b0\u4ee3\u67b6\u6784\u4e2d&#xff0c;\u7ebf\u7a0b\u57fa\u672c\u90fd\u662f\u7ebf\u7a0b\u6c60\u7ba1\u7406\u7684&#xff0c;\u5bff\u547d\u6781\u957f&#xff01;\u8fd9\u4f1a\u5bfc\u81f4\u5927\u91cf\u65e9\u5c31\u4e0d\u7528\u7684\u5783\u573e\u6570\u636e\u5806\u79ef\u3002 \u6539\u4e3a Thread \u7ef4\u62a4\u540e&#xff0c;Map \u968f\u7ebf\u7a0b\u540c\u751f\u5171\u6b7b\u3002\u4e00\u65e6\u7ebf\u7a0b\u9500\u6bc1&#xff0c;\u5b83\u81ea\u5e26\u7684 ThreadLocalMap \u5c31\u4f1a\u8fde\u540c\u91cc\u9762\u7684 Value \u4e00\u8d77\u88ab GC \u81ea\u52a8\u56de\u6536&#xff0c;\u6781\u5927\u964d\u4f4e\u4e86\u5185\u5b58\u6cc4\u6f0f\u7684\u98ce\u9669\u3002<\/li>\n<h4>\u4e09\u3001\u6b7b\u78d5 ThreadLocal \u6e90\u7801\u4e0e\u5185\u5b58\u6cc4\u6f0f<\/h4>\n<h5>&#x1f534; 1. Hash \u51b2\u7a81\u4e0e\u7ebf\u6027\u63a2\u6d4b\u6cd5&#xff08;\u4e3a\u4ec0\u4e48\u4e0d\u7528\u94fe\u8868&#xff1f;&#xff09;<\/h5>\n<p>\u65e2\u7136\u524d\u4e00\u7ae0\u8bf4\u4e86&#xff0c;ThreadLocalMap \u662f\u5f53\u524d\u7ebf\u7a0b\u53e3\u888b\u91cc\u7684\u5c0f\u8d26\u672c&#xff0c;\u90a3\u5b83\u662f\u600e\u4e48\u8bb0\u8d26\u7684\u5462&#xff1f; \u5b83\u5176\u5b9e\u662f\u4e00\u4e2a\u6781\u5176\u7b80\u5355\u7684 Entry[] \u6570\u7ec4\u3002\u6ca1\u6709\u50cf HashMap \u90a3\u6837\u590d\u6742\u7684\u94fe\u8868\u548c\u7ea2\u9ed1\u6811\u3002<\/p>\n<p>**\u6590\u6ce2\u90a3\u5951\u6563\u5217&#xff1a;\u795e\u5947\u7684\u9b54\u6570 ****0x61c88647** \u6bcf\u6b21\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 ThreadLocal&#xff0c;\u5b83\u5185\u90e8\u5c31\u4f1a\u5206\u914d\u4e00\u4e2a Hash \u503c\u3002\u6e90\u7801\u91cc\u662f\u8fd9\u4e48\u5199\u7684&#xff1a;<\/p>\n<p>Java<\/p>\n<p>\/\/ \u6bcf\u6b21\u521b\u5efa\u5bf9\u8c61&#xff0c;\u7d2f\u52a0\u8fd9\u4e2a\u9b54\u6570<br \/>\nprivate static final int HASH_INCREMENT &#061; 0x61c88647;<br \/>\nprivate static int nextHashCode() {<br \/>\n    return nextHashCode.getAndAdd(HASH_INCREMENT);<br \/>\n}<\/p>\n<p>\u8fd9\u4e2a\u9b54\u6570 0x61c88647 \u662f\u6590\u6ce2\u90a3\u5951\u6570&#xff08;\u9ec4\u91d1\u5206\u5272\u6570&#xff09;\u7684\u4e00\u4e2a\u53d8\u79cd\u3002\u4e00\u53e5\u8bdd\u8bb2\u660e\u767d\u5b83\u7684\u4f5c\u7528&#xff1a;\u65e0\u8bba\u521b\u5efa\u591a\u5c11\u4e2a **ThreadLocal**&#xff0c;\u6bcf\u6b21\u628a\u8fd9\u4e2a Hash \u503c\u548c\u6570\u7ec4\u5927\u5c0f&#xff08;2\u7684n\u6b21\u5e42&#xff09;\u505a\u4f4d\u8fd0\u7b97\u540e&#xff0c;\u90fd\u80fd\u6781\u5176\u5747\u5300\u5730\u6563\u5e03\u5728\u6570\u7ec4\u4e2d&#xff0c;\u6781\u5927\u5730\u51cf\u5c11\u4e86 Hash \u51b2\u7a81\u3002<\/p>\n<p>\u7ebf\u6027\u63a2\u6d4b\u6cd5&#xff1a;\u627e\u5ea7\u4f4d\u7684\u54f2\u5b66 \u867d\u7136\u6563\u5217\u5747\u5300&#xff0c;\u4f46\u51b2\u7a81\u8fd8\u662f\u4e0d\u53ef\u907f\u514d\u7684\u3002HashMap \u9047\u5230\u51b2\u7a81\u662f\u5728\u539f\u4f4d\u7f6e\u6302\u4e2a\u94fe\u8868&#xff08;\u62c9\u94fe\u6cd5&#xff09;&#xff0c;\u800c ThreadLocalMap \u91c7\u7528\u7684\u662f\u7ebf\u6027\u63a2\u6d4b\u6cd5&#xff08;\u5f00\u653e\u5bfb\u5740\u6cd5&#xff09;\u3002<\/p>\n<p>\u6253\u4e2a\u6bd4\u65b9&#xff1a;\u4f60\u53bb\u7535\u5f71\u9662\u627e\u5ea7\u4f4d&#xff08;\u7ecf\u8fc7 Hash \u8ba1\u7b97\u4f60\u662f 5 \u53f7\u5ea7&#xff09;\u3002<\/p>\n<li>\u8d70\u5230 5 \u53f7\u5ea7&#xff0c;\u53d1\u73b0\u6ca1\u4eba&#xff0c;\u5750\u4e0b\u3002<\/li>\n<li>\u5982\u679c 5 \u53f7\u5ea7\u6709\u4eba\u4e86&#xff08;Hash \u51b2\u7a81&#xff09;&#xff0c;\u4f60\u5c31\u4e0d\u6302\u5728\u4ed6\u8eab\u4e0a\u4e86&#xff0c;\u800c\u662f\u5f80\u540e\u770b 6 \u53f7\u5ea7\u3002<\/li>\n<li>\u5982\u679c 6 \u53f7\u4e5f\u6709\u4eba&#xff0c;\u7ee7\u7eed\u770b 7 \u53f7&#xff0c;\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u7a7a\u5ea7\u4f4d\u5750\u4e0b\u3002\u5982\u679c\u8d70\u5230\u4e86\u6700\u540e\u4e00\u6392&#xff0c;\u5c31\u4ece\u7b2c 1 \u6392\u91cd\u65b0\u627e&#xff08;\u73af\u5f62\u6570\u7ec4&#xff09;\u3002<\/li>\n<p>\u8fd9\u5957\u673a\u5236\u7684\u4ee3\u7801\u5f02\u5e38\u7cbe\u7b80&#xff1a;<\/p>\n<p>Java<\/p>\n<p>private void set(ThreadLocal&lt;?&gt; key, Object value) {<br \/>\n    \/\/ 1. \u628a\u5c0f\u8d26\u672c\u62ff\u51fa\u6765<br \/>\n    Entry[] tab &#061; table; <\/p>\n<p>    \/\/ 2. \u3010\u8ba1\u7b97\u5ea7\u4f4d\u53f7\u3011<br \/>\n    \/\/ \u7528 ThreadLocal \u7684 hash \u503c\u548c\u6570\u7ec4\u957f\u5ea6\u505a\u4f4d\u8fd0\u7b97&#xff08;\u76f8\u5f53\u4e8e\u53d6\u6a21&#xff09;<br \/>\n    \/\/ \u5047\u8bbe\u7b97\u51fa\u6765 i &#061; 1<br \/>\n    int i &#061; key.threadLocalHashCode &amp; (tab.length &#8211; 1);<\/p>\n<p>    \/\/ 3. \u3010\u627e\u5ea7\u4f4d\u5faa\u73af\u3011<br \/>\n    \/\/ \u8d70\u5230\u7b2c i \u4e2a\u5ea7\u4f4d&#xff08;\u6b64\u65f6 i&#061;1&#xff09;&#xff0c;\u770b\u770b\u6709\u6ca1\u6709\u4eba\u5750\u7740\u3002<br \/>\n    \/\/ \u5982\u679c e !&#061; null&#xff08;\u6709\u4eba\u5750&#xff09;&#xff0c;\u5c31\u8fdb\u5165\u5faa\u73af\u4f53&#xff1b;\u5982\u679c e &#061;&#061; null&#xff08;\u6ca1\u4eba\u5750&#xff09;&#xff0c;\u76f4\u63a5\u8df3\u51fa\u5faa\u73af&#xff01;<br \/>\n    \/\/ \u6bcf\u6b21\u5faa\u73af\u7ed3\u675f&#xff0c;i \u5c31\u4f1a\u901a\u8fc7 nextIndex \u5f80\u540e\u632a\u4e00\u4f4d&#xff08;1\u53d8\u62102&#xff0c;2\u53d8\u62103&#8230;&#xff09;<br \/>\n    for (Entry e &#061; tab[i]; e !&#061; null; e &#061; tab[i &#061; nextIndex(i, len)]) {<\/p>\n<p>        \/\/ &#8212; \u5faa\u73af\u4f53\u5185\u90e8 &#8212;<\/p>\n<p>        \/\/ \u53d1\u73b0\u5ea7\u4f4d\u4e0a\u6709\u4eba\u3002\u770b\u770b\u8fd9\u4e2a\u4eba\u7684 Key \u662f\u4e0d\u662f\u6211\u81ea\u5df1&#xff1f;<br \/>\n        if (e.get() &#061;&#061; key) {<br \/>\n            \/\/ \u54ce\u5440&#xff0c;\u539f\u6765\u662f\u6211\u81ea\u5df1\u4e4b\u524d\u5360\u7684\u5ea7&#xff01;<br \/>\n            \/\/ \u8bf4\u660e\u6211\u662f\u6765\u66f4\u65b0\u6570\u636e\u7684&#xff0c;\u76f4\u63a5\u628a\u65e7 Value \u66ff\u6362\u6210\u65b0 Value<br \/>\n            e.value &#061; value;<br \/>\n            return; \/\/ \u641e\u5b9a&#xff0c;\u4e0b\u73ed&#xff01;<br \/>\n        }<\/p>\n<p>        \/\/ \u5982\u679c e.get() !&#061; key&#xff0c;\u8bf4\u660e\u5ea7\u4f4d\u4e0a\u5750\u7684\u662f\u522b\u4eba&#xff08;\u6bd4\u5982 ThreadLocal A&#xff09;\u3002<br \/>\n        \/\/ \u8fd9\u5c31\u662f\u53d1\u751f\u4e86\u3010Hash\u51b2\u7a81\u3011&#xff01;<br \/>\n        \/\/ \u90a3\u600e\u4e48\u529e&#xff1f;\u5565\u4e5f\u4e0d\u5e72&#xff0c;\u987a\u7740 for \u5faa\u73af\u7684 e &#061; tab[i &#061; nextIndex(i, len)]&#xff0c;<br \/>\n        \/\/ \u8d70\u5230\u4e0b\u4e00\u4e2a\u5ea7\u4f4d&#xff08;i&#061;2&#xff09;\u7ee7\u7eed\u770b&#xff01;<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u3010\u843d\u5ea7\u3011<br \/>\n    \/\/ \u80fd\u8d70\u5230\u8fd9\u91cc&#xff0c;\u8bf4\u660e for \u5faa\u73af\u78b0\u5230\u4e86 e &#061;&#061; null \u7684\u60c5\u51b5&#xff0c;\u8df3\u51fa\u4e86\u5faa\u73af\u3002<br \/>\n    \/\/ \u4e5f\u5c31\u662f\u627e\u5230\u4e86\u4e00\u4e2a\u3010\u7a7a\u5ea7\u4f4d\u3011\u3002<br \/>\n    \/\/ \u90a3\u5c31\u76f4\u63a5 new \u4e00\u4e2a\u65b0\u7684 Entry&#xff0c;\u4e00\u5c41\u80a1\u5750\u4e0b\u53bb&#xff01;<br \/>\n    tab[i] &#061; new Entry(key, value);<\/p>\n<p>    \/\/ &#8230; \u68c0\u67e5\u9700\u4e0d\u9700\u8981\u6269\u5bb9&#xff08;\u6682\u4e0d\u5c55\u5f00&#xff09;<br \/>\n}<\/p>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u7528\u94fe\u8868&#xff1f;<\/p>\n<p>\u4e0d\u7528\u94fe\u8868&#xff0c;\u662f\u56e0\u4e3a ThreadLocal \u6570\u91cf\u5c11\u4e14 Hash \u5206\u5e03\u5747\u5300&#xff0c;\u51b2\u7a81\u6781\u5c11&#xff1b;\u800c\u5728\u51b2\u7a81\u6781\u5c11\u7684\u524d\u63d0\u4e0b&#xff0c;\u8fde\u7eed\u6570\u7ec4\u5e26\u6765\u7684 CPU \u9ad8\u901f\u7f13\u5b58\u547d\u4e2d\u7387 \u548c \u6781\u7b80\u7684\u5783\u573e\u6e05\u7406\u903b\u8f91&#xff0c;\u8ba9\u7ebf\u6027\u63a2\u6d4b\u6cd5\u6210\u4e3a\u4e86\u8fd9\u4e2a\u573a\u666f\u4e0b\u78be\u538b\u94fe\u8868\u7684\u6700\u4f18\u89e3\u3002<\/p>\n<hr \/>\n<h5>&#x1f534; 2. \u6838\u5fc3\u91cd\u70b9&#xff1a;ThreadLocal \u5185\u5b58\u6cc4\u6f0f\u7684\u6839\u672c\u539f\u56e0<\/h5>\n<p>\u8fd9\u662f\u5168\u7bc7\u7684\u6700\u9ad8\u6f6e\u3002\u5982\u679c\u9762\u8bd5\u5b98\u95ee ThreadLocal&#xff0c;\u8fd9\u9053\u9898\u7684\u51fa\u73b0\u6982\u7387\u662f 100%\u3002<\/p>\n<p>\u5728 ThreadLocalMap \u4e2d&#xff0c;Entry \u7684\u5b9a\u4e49\u662f\u8fd9\u6837\u7684&#xff1a;<\/p>\n<p>Java<\/p>\n<p>\/\/ Entry \u7ee7\u627f\u4e86 WeakReference&#xff0c;\u8fd9\u610f\u5473\u7740\u5b83\u7684 Key (ThreadLocal) \u662f\u4e00\u4e2a\u5f31\u5f15\u7528<br \/>\nstatic class Entry extends WeakReference&lt;ThreadLocal&lt;?&gt;&gt; {<br \/>\n    Object value; \/\/ Value \u4f9d\u7136\u662f\u5f3a\u5f15\u7528<br \/>\n    Entry(ThreadLocal&lt;?&gt; k, Object v) {<br \/>\n        super(k);<br \/>\n        value &#061; v;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u4e3a\u4ec0\u4e48 Key \u8981\u8bbe\u8ba1\u6210\u5f31\u5f15\u7528&#xff1f;\u5f3a\u5f15\u7528\u4e0d\u884c\u5417&#xff1f; \u5047\u8bbe\u6211\u4eec\u5728\u4e1a\u52a1\u4ee3\u7801\u91cc\u5199\u4e86 ThreadLocal tl &#061; new ThreadLocal()\u3002\u5f53\u4e1a\u52a1\u7ed3\u675f&#xff0c;\u6211\u4eec\u628a tl &#061; null&#xff0c;\u672c\u610f\u662f\u5e0c\u671b\u56de\u6536\u8fd9\u4e2a\u5bf9\u8c61\u3002<\/p>\n<ul>\n<li>\u5982\u679c Key \u662f\u5f3a\u5f15\u7528&#xff1a;\u5373\u4f7f\u5916\u90e8 tl &#061; null&#xff0c;\u5f53\u524d\u7ebf\u7a0b\u7684 ThreadLocalMap \u91cc\u7684 Entry \u4f9d\u7136\u5f3a\u5f15\u7528\u7740\u8fd9\u4e2a ThreadLocal\u3002\u53ea\u8981\u7ebf\u7a0b\u4e0d\u6b7b&#xff0c;\u8fd9\u4e2a\u5bf9\u8c61\u6c38\u8fdc\u65e0\u6cd5\u88ab GC \u56de\u6536&#xff01;<\/li>\n<li>\u5982\u679c Key \u662f\u5f31\u5f15\u7528&#xff1a;\u5f31\u5f15\u7528\u7684\u7279\u6027\u662f\u201c\u53ea\u8981\u53d1\u751f GC&#xff0c;\u5c31\u4f1a\u88ab\u65e0\u60c5\u56de\u6536\u201d\u3002\u6240\u4ee5\u4e00\u65e6\u5916\u90e8\u5f15\u7528\u65ad\u5f00&#xff0c;\u4e0b\u4e00\u6b21 GC \u5c31\u4f1a\u628a Entry \u7684 Key \u56de\u6536\u6389&#xff0c;Key \u5c31\u53d8\u6210\u4e86 null\u3002<\/li>\n<\/ul>\n<p>\u81f4\u547d\u7684\u6cc4\u6f0f\u6210\u56e0\u5256\u6790 Key \u53d8\u6210 null \u597d\u50cf\u5f88\u5b8c\u7f8e&#xff1f;\u5927\u9519\u7279\u9519&#xff01;\u8fd9\u4e5f\u662f\u5927\u5382\u6781\u5176\u5bb9\u6613\u8e29\u5751\u7684\u5730\u65b9\u3002<\/p>\n<p>\u770b\u770b\u73b0\u5728\u7684\u5f15\u7528\u94fe&#xff1a; CurrentThread -&gt; ThreadLocalMap -&gt; Entry -&gt; Value \u8fd9\u6761\u94fe\u4e0a\u5168\u90e8\u90fd\u662f\u5f3a\u5f15\u7528&#xff01;<\/p>\n<p>\u867d\u7136 Key \u88ab\u56de\u6536\u53d8\u6210\u4e86 null&#xff08;\u8fd9\u79cd Entry \u88ab\u79f0\u4e3a\u810f\u6570\u636e Stale Entry&#xff09;&#xff0c;\u4f46\u662f Value \u4f9d\u7136\u88ab\u5f3a\u5f15\u7528\u7740\u3002\u5728\u73b0\u4ee3\u67b6\u6784\u4e2d&#xff0c;\u6211\u4eec\u57fa\u672c\u90fd\u5728\u4f7f\u7528\u7ebf\u7a0b\u6c60&#xff0c;\u6838\u5fc3\u7ebf\u7a0b\u662f\u4e0d\u4f1a\u88ab\u9500\u6bc1\u7684\u3002 \u8fd9\u5c31\u610f\u5473\u7740&#xff0c;\u53ea\u8981\u7ebf\u7a0b\u5728\u6c60\u5b50\u91cc\u4e0d\u9500\u6bc1&#xff0c;\u8fd9\u4e2a Value \u7684\u5f3a\u5f15\u7528\u94fe\u5c31\u4e00\u76f4\u65ad\u4e0d\u6389&#xff0c;\u6c38\u8fdc\u65e0\u6cd5\u88ab GC \u751a\u81f3\u4f1a\u8d8a\u5806\u8d8a\u591a&#xff0c;\u6700\u7ec8\u5bfc\u81f4 OOM&#xff08;\u5185\u5b58\u6ea2\u51fa&#xff09;\u3002<\/p>\n<p>&#x1f4a1;** \u9762\u8bd5\u5b98**&#xff1a;\u65e2\u7136\u8bbe\u8ba1\u6210\u5f31\u5f15\u7528\u8fd8\u662f\u4f1a\u5bfc\u81f4 Value \u6cc4\u6f0f&#xff0c;\u90a3\u515c\u4e86\u4e00\u5927\u5708&#xff0c;Doug Lea \u628a Key \u8bbe\u8ba1\u6210\u5f31\u5f15\u7528\u7684\u610f\u4e49\u5230\u5e95\u662f\u4ec0\u4e48&#xff1f;\u4e24\u8fb9\u4e0d\u90fd\u662f\u5751\u5417&#xff1f;<\/p>\n<p>\u56de\u7b54&#xff1a;\u8fd9\u662f\u5178\u578b\u7684\u201c\u4e24\u5bb3\u76f8\u6743\u53d6\u5176\u8f7b\u201d\u3002 \u5982\u679c\u7528\u5f3a\u5f15\u7528&#xff0c;\u90a3\u4e48 Key \u548c Value \u4e00\u5b9a\u4f1a\u4e00\u8d77\u6cc4\u6f0f&#xff0c;\u4e14\u5e95\u5c42\u6beb\u65e0\u529e\u6cd5\u3002 \u4f46\u7528\u4e86\u5f31\u5f15\u7528&#xff0c;\u867d\u7136 Value \u4f1a\u6cc4\u6f0f&#xff0c;\u4f46\u81f3\u5c11 Key \u88ab\u56de\u6536\u4e86&#xff0c;\u8fd9\u5c31\u7ed9\u5e95\u5c42\u7559\u4e0b\u4e86\u4e00\u4e2a\u660e\u663e\u7684\u6807\u8bb0&#xff08;Key \u4e3a null&#xff09;\u3002Doug Lea \u6b63\u662f\u5229\u7528\u8fd9\u4e2a\u201c\u6807\u8bb0\u201d&#xff0c;\u5728 ThreadLocal \u7684 get()\u3001set() \u64cd\u4f5c\u4e2d&#xff0c;\u57cb\u5165\u4e86\u987a\u624b\u6e05\u7406\u201c\u810f\u6570\u636e\u201d&#xff08;Key \u4e3a null \u7684 Entry&#xff09;\u7684\u903b\u8f91\u3002\u5f31\u5f15\u7528\u76f8\u5f53\u4e8e\u4e3a\u5f00\u53d1\u8005\u5fd8\u8bb0\u624b\u52a8 remove() \u63d0\u4f9b\u4e86\u6700\u540e\u4e00\u9053\u9632\u7ebf\u3002<\/p>\n<p>&#x1f6a8;** \u771f\u5b9e\u4e1a\u52a1\u9632\u5751\u6307\u5357** \u4e0d\u8981\u8fc7\u4e8e\u8ff7\u4fe1\u5e95\u5c42\u7684\u201c\u987a\u624b\u6e05\u7406\u201d&#xff0c;\u56e0\u4e3a\u5982\u679c\u4f60\u540e\u7eed\u4e0d\u518d\u8c03\u7528\u8fd9\u4e2a\u7ebf\u7a0b\u7684 get\/set&#xff0c;\u6e05\u7406\u903b\u8f91\u538b\u6839\u4e0d\u4f1a\u89e6\u53d1\u3002 \u6700\u89c4\u8303\u3001\u552f\u4e00\u5b89\u5168\u7684\u5199\u6cd5&#xff1a;\u5728\u6bcf\u6b21\u4f7f\u7528\u5b8c ThreadLocal \u540e&#xff0c;\u52a1\u5fc5\u5728 finally \u5757\u4e2d\u8c03\u7528 remove()&#xff01;\u8fd9\u5c31\u597d\u6bd4\u4f60\u501f\u4e86\u516c\u53f8\u7684\u516c\u8f66&#xff0c;\u4e0b\u73ed\u524d\u5fc5\u987b\u62d4\u6389\u8f66\u94a5\u5319\u8fd8\u56de\u8f66\u5e93\u3002<\/p>\n<p>Java<\/p>\n<p>\/\/ \u6781\u5176\u91cd\u8981\u7684\u4e1a\u52a1\u89c4\u8303<br \/>\ntry {<br \/>\n    threadLocal.set(user);<br \/>\n    \/\/ \u6267\u884c\u4e1a\u52a1\u903b\u8f91&#8230;<br \/>\n} finally {<br \/>\n    threadLocal.remove(); \/\/ \u5f3a\u5236\u624b\u52a8\u6e05\u7406&#xff01;<br \/>\n}<\/p>\n<hr \/>\n<h5>&#x1f534; 3. \u6e90\u7801\u7ec6\u8282&#xff1a;Doug Lea \u7684\u5783\u573e\u6e05\u7406\u673a\u5236 (\u9009\u8bfb\/\u62d4\u9ad8)<\/h5>\n<p>\u521a\u624d\u63d0\u5230&#xff0c;\u5e95\u5c42\u7684 set() \u548c get() \u4f1a\u201c\u987a\u624b\u6e05\u7406\u201d\u810f\u6570\u636e\u3002\u5982\u679c\u4f60\u80fd\u7ed9\u9762\u8bd5\u5b98\u8bb2\u6e05\u695a\u8fd9\u4e2a\u673a\u5236&#xff0c;\u606d\u559c\u4f60&#xff0c;\u4f60\u7684 Offer \u5df2\u7ecf\u7a33\u4e86\u3002<\/p>\n<p>\u6e05\u7406\u4e3b\u8981\u9760\u4e24\u4e2a\u6838\u5fc3\u65b9\u6cd5\u76f8\u4e92\u914d\u5408&#xff1a;\u63a2\u6d4b\u5f0f\u6e05\u7406 (**expungeStaleEntry**) \u548c \u542f\u53d1\u5f0f\u6e05\u7406 (**cleanSomeSlots**)\u3002<\/p>\n<p>\u63a2\u6d4b\u5f0f\u6e05\u7406&#xff08;\u6e05\u9053\u592b\u626b\u5927\u8857&#xff09;&#xff1a; \u5f53\u4f60\u5728\u904d\u5386\u6570\u7ec4\u627e\u5ea7\u4f4d\u65f6&#xff0c;\u5982\u679c\u53d1\u73b0\u4e86\u4e00\u4e2a Key &#061;&#061; null \u7684\u810f Entry&#xff0c;\u7cfb\u7edf\u4f1a\u7acb\u523b\u5316\u8eab\u6e05\u9053\u592b\u3002<\/p>\n<li>\u628a\u8fd9\u4e2a\u810f Entry \u7684 Value \u548c Entry \u81ea\u8eab\u90fd\u8bbe\u7f6e\u4e3a null&#xff08;\u5f7b\u5e95\u65a9\u65ad\u5f3a\u5f15\u7528&#xff0c;Help GC&#xff09;\u3002<\/li>\n<li>\u91cd\u54c8\u5e0c&#xff08;Re-hash&#xff09;&#xff1a;\u6e05\u9053\u592b\u4e0d\u4f1a\u626b\u5b8c\u8fd9\u4e2a\u5751\u5c31\u8d70&#xff0c;\u4ed6\u4f1a\u7ee7\u7eed\u5f80\u540e\u626b&#xff0c;\u628a\u540e\u9762\u90a3\u4e9b\u56e0\u4e3a Hash \u51b2\u7a81\u800c\u88ab\u8feb\u201c\u5f80\u540e\u5750\u201d\u7684\u5143\u7d20&#xff0c;\u91cd\u65b0\u5f80\u524d\u632a\u3002\u4e0d\u4ec5\u6e05\u7406\u4e86\u5783\u573e&#xff0c;\u8fd8\u8ba9\u6563\u5217\u8868\u66f4\u52a0\u7d27\u51d1&#xff0c;\u63d0\u9ad8\u4e86\u4e0b\u6b21\u67e5\u8be2\u7684\u6548\u7387\u3002<\/li>\n<p>\u542f\u53d1\u5f0f\u6e05\u7406&#xff08;\u968f\u624b\u6254\u5783\u573e&#xff09;&#xff1a; \u5728 set() \u6210\u529f\u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20\u540e&#xff0c;\u7cfb\u7edf\u4e0d\u4f1a\u5168\u76d8\u626b\u63cf&#xff08;\u592a\u8017\u6027\u80fd&#xff09;&#xff0c;\u800c\u662f\u505a\u4e00\u6b21\u5bf9\u6570\u7ea7\u522b&#xff08;O(logN)&#xff09;\u7684\u8f7b\u91cf\u7ea7\u626b\u63cf\u3002\u5982\u679c\u626b\u5230\u4e86\u810f\u6570\u636e&#xff0c;\u5c31\u5524\u9192\u201c\u63a2\u6d4b\u5f0f\u6e05\u7406\u201d\u53bb\u5927\u5e72\u4e00\u573a&#xff1b;\u5982\u679c\u8fde\u7eed\u626b\u4e86\u51e0\u6b21\u90fd\u6ca1\u53d1\u73b0\u810f\u6570\u636e&#xff0c;\u5c31\u8ba4\u4e3a\u5f53\u524d\u73af\u5883\u6bd4\u8f83\u5e72\u51c0&#xff0c;\u76f4\u63a5\u4e0b\u73ed\u3002<\/p>\n<p>\u8fd9\u79cd**\u201c\u5728\u5e73\u65f6\u8bfb\u5199\u64cd\u4f5c\u4e2d\u5939\u5e26\u79c1\u8d27\u8fdb\u884c\u8f7b\u91cf\u7ea7\u6e05\u7406\u201d**\u7684\u7cbe\u5999\u8bbe\u8ba1&#xff0c;\u5145\u5206\u4f53\u73b0\u4e86\u5e76\u53d1\u5927\u5e08\u5bf9\u4e8e\u6027\u80fd\u548c\u5185\u5b58\u5e73\u8861\u7684\u6781\u81f4\u8ffd\u6c42\u3002<\/p>\n<p>**\u603b\u7ed3&#xff1a;**ThreadLocal \u7684\u5f31\u5f15\u7528\u662f\u4e3a\u4e86\u5728\u5f00\u53d1\u8005\u5fd8\u8bb0 remove \u65f6&#xff0c;\u901a\u8fc7 GC \u5c06 Key \u7f6e\u4e3a null \u7559\u4e0b\u8bb0\u53f7&#xff0c;\u4f7f\u5f97\u540e\u7eed\u8c03\u7528 get\/set \u65f6\u80fd\u89e6\u53d1\u5e95\u5c42\u88ab\u52a8\u6e05\u7406 Value\u3002\u4f46\u5728\u7ebf\u7a0b\u6c60\u73af\u5883\u4e0b&#xff0c;\u7531\u4e8e\u7ebf\u7a0b\u590d\u7528\u4e14\u4e0d\u4e00\u5b9a\u518d\u6b21\u8c03\u7528 get\/set&#xff0c;\u8fd9\u79cd\u88ab\u52a8\u6e05\u7406\u5e76\u4e0d\u53ef\u9760&#xff0c;\u5fc5\u987b\u5728\u4e1a\u52a1 finally \u5757\u4e2d\u4e3b\u52a8\u8c03\u7528 remove() \u624d\u662f\u9632\u6cc4\u6f0f\u7684\u6839\u672c\u65b9\u6848\u3002<\/p>\n<h4>\u56db\u3001\u8fdb\u9636\u5ef6\u4f38 \u2014\u2014 \u8de8\u7ebf\u7a0b\u7684\u6570\u636e\u900f\u4f20<\/h4>\n<h5>&#x1f7e1; 1. \u539f\u7406\u5e94\u7528&#xff1a;\u7236\u5b50\u7ebf\u7a0b\u4f20\u9012 InheritableThreadLocal<\/h5>\n<p>\u4e3a\u4e86\u89e3\u51b3\u7236\u5b50\u7ebf\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012&#xff0c;JDK \u5b98\u65b9\u63d0\u4f9b\u4e86\u4e00\u4e2a ThreadLocal \u7684\u5b50\u7c7b&#xff1a;**InheritableThreadLocal**&#xff08;\u53ef\u7ee7\u627f\u7684\u672c\u5730\u7ebf\u7a0b\u53d8\u91cf&#xff09;\u3002<\/p>\n<p>\u6253\u4e2a\u6bd4\u65b9&#xff1a;\u5f20\u4e09&#xff08;\u7236\u7ebf\u7a0b&#xff09;\u81ea\u5df1\u6709\u4e2a\u5c0f\u8d26\u672c&#xff0c;\u4eca\u5929\u4ed6\u62db\u4e86\u4e2a\u52a9\u7406&#xff08;\u5b50\u7ebf\u7a0b&#xff09;\u3002\u4e3a\u4e86\u8ba9\u52a9\u7406\u80fd\u63a5\u624b\u5de5\u4f5c&#xff0c;\u5f20\u4e09\u5728**\u529e\u7406\u5165\u804c&#xff08;\u521b\u5efa\u5b50\u7ebf\u7a0b&#xff09;**\u7684\u65f6\u5019&#xff0c;\u7279\u610f\u53bb\u590d\u5370\u4e86\u4e00\u4efd\u81ea\u5df1\u7684\u8d26\u672c&#xff0c;\u585e\u8fdb\u4e86\u52a9\u7406\u7684\u53e3\u888b\u91cc\u3002<\/p>\n<p>\u7528\u6cd5\u6781\u5176\u7b80\u5355&#xff0c;\u53ea\u9700\u8981\u628a ThreadLocal \u66ff\u6362\u6210 InheritableThreadLocal \u5373\u53ef&#xff1a;<\/p>\n<p>Java<\/p>\n<p>public class ITLDemo {<br \/>\n    static InheritableThreadLocal&lt;String&gt; itl &#061; new InheritableThreadLocal&lt;&gt;();<\/p>\n<p>    public static void main(String[] args) {<br \/>\n        itl.set(&#034;\u7236\u7ebf\u7a0b\u7684TraceId-10086&#034;);<\/p>\n<p>        \/\/ \u5f00\u542f\u5b50\u7ebf\u7a0b<br \/>\n        new Thread(() -&gt; {<br \/>\n            \/\/ \u5b50\u7ebf\u7a0b\u795e\u5947\u5730\u62ff\u5230\u4e86\u7236\u7ebf\u7a0b\u7684\u6570\u636e&#xff01;<br \/>\n            System.out.println(&#034;\u5b50\u7ebf\u7a0b\u83b7\u53d6: &#034; &#043; itl.get());<br \/>\n        }).start();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u5e95\u5c42\u662f\u600e\u4e48\u505a\u5230\u7684&#xff1f;<\/p>\n<p>\u79d8\u5bc6\u5c31\u85cf\u5728 new Thread() \u7684\u521d\u59cb\u5316\u6e90\u7801\u91cc\u3002\u5728 Thread \u7c7b\u4e2d&#xff0c;\u9664\u4e86\u6211\u4eec\u8bb2\u7684 threadLocals \u5c5e\u6027\u5916&#xff0c;\u8fd8\u6709\u4e00\u4e2a\u4e13\u95e8\u7528\u6765\u505a\u7236\u5b50\u900f\u4f20\u7684\u5c5e\u6027 inheritableThreadLocals\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u65f6&#xff0c;\u5e95\u5c42\u5fc5\u7136\u4f1a\u8c03\u7528 Thread#init \u65b9\u6cd5&#xff1a;<\/p>\n<p>Java<\/p>\n<p>\/\/ Thread \u7c7b\u521d\u59cb\u5316\u6e90\u7801&#xff08;\u6781\u7b80\u7248&#xff09;<br \/>\nprivate void init(ThreadGroup g, Runnable target, String name, long stackSize, AccessControlContext acc, boolean inheritThreadLocals) {<br \/>\n    \/\/ 1. \u83b7\u53d6\u5f53\u524d\u6b63\u5728\u6267\u884c\u7684\u7ebf\u7a0b&#xff08;\u4e5f\u5c31\u662f\u5373\u5c06\u4f5c\u4e3a\u7236\u7ebf\u7a0b\u7684\u90a3\u4e2a\u7ebf\u7a0b&#xff09;<br \/>\n    Thread parent &#061; currentThread();<\/p>\n<p>    \/\/ 2. \u5982\u679c\u7236\u7ebf\u7a0b\u7684 inheritableThreadLocals \u4e0d\u4e3a\u7a7a&#xff0c;\u8bf4\u660e\u6709\u6570\u636e\u9700\u8981\u5411\u4e0b\u4f20<br \/>\n    if (inheritThreadLocals &amp;&amp; parent.inheritableThreadLocals !&#061; null) {<br \/>\n        \/\/ 3. \u6838\u5fc3\u5927\u62db&#xff1a;\u521b\u5efa\u5b50\u7ebf\u7a0b\u7684 Map&#xff0c;\u5e76\u628a\u7236\u7ebf\u7a0b Map \u91cc\u7684 Entry \u9010\u4e2a\u590d\u5236\u8fc7\u6765&#xff01;<br \/>\n        this.inheritableThreadLocals &#061; ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u7b80\u5355\u6765\u8bf4&#xff0c;\u5c31\u662f\u5728\u521b\u5efa\u5b50\u7ebf\u7a0b\u7684\u90a3\u4e00\u77ac\u95f4&#xff0c;JVM \u987a\u624b\u628a\u7236\u7ebf\u7a0b InheritableThreadLocal \u91cc\u7684\u6570\u636e\u6d45\u62f7\u8d1d\u4e86\u4e00\u4efd\u7ed9\u5b50\u7ebf\u7a0b\u3002<\/p>\n<hr \/>\n<h5>&#x1f7e2; 2. \u6280\u672f\u5ef6\u4f38&#xff1a;\u7ebf\u7a0b\u6c60\u73af\u5883\u4e0b\u7684\u5931\u6548\u5371\u673a<\/h5>\n<p>InheritableThreadLocal \u770b\u8d77\u6765\u5f88\u7f8e&#xff0c;\u4f46\u5728\u73b0\u4ee3 Java \u5f00\u53d1\u4e2d&#xff0c;\u6211\u4eec\u51e0\u4e4e\u4e0d\u4f1a\u7528\u5b83\u3002\u539f\u56e0\u5f88\u7b80\u5355&#xff1a;\u6211\u4eec\u73b0\u5728\u6839\u672c\u4e0d\u4f1a\u624b\u52a8 **new Thread()**&#xff0c;\u800c\u662f\u5168\u90fd\u7528\u7ebf\u7a0b\u6c60&#xff01;<\/p>\n<p>\u8fd9\u91cc\u9762\u6709\u4e00\u4e2a\u81f4\u547d\u7684\u903b\u8f91\u6f0f\u6d1e&#xff1a; \u521a\u624d\u6211\u4eec\u770b\u4e86\u6e90\u7801&#xff0c;\u7236\u5b50\u7ebf\u7a0b\u7684\u6570\u636e\u590d\u5236\u53ea\u53d1\u751f\u5728 **new Thread()** \u521d\u59cb\u5316\u7684\u4e00\u77ac\u95f4\u3002 \u800c\u5728\u7ebf\u7a0b\u6c60&#xff08;\u5982 Tomcat \u5de5\u4f5c\u7ebf\u7a0b\u6c60\u3001\u4e1a\u52a1\u5f02\u6b65\u7ebf\u7a0b\u6c60&#xff09;\u4e2d&#xff0c;\u6838\u5fc3\u7ebf\u7a0b\u662f\u88ab\u53cd\u590d\u590d\u7528\u7684\u3002<\/p>\n<p>\u707e\u96be\u573a\u666f\u6f14\u793a&#xff1a;<\/p>\n<li>\u8bf7\u6c42 A \u8fdb\u6765&#xff0c;\u7ebf\u7a0b\u6c60\u65b0\u5efa\u4e86\u201c\u7ebf\u7a0b 1\u201d&#xff0c;\u5b8c\u7f8e\u7ee7\u627f\u4e86\u8bf7\u6c42 A \u7684 TraceId\u3002<\/li>\n<li>\u8bf7\u6c42 A \u7ed3\u675f&#xff0c;\u201c\u7ebf\u7a0b 1\u201d\u6ca1\u6709\u88ab\u9500\u6bc1&#xff0c;\u800c\u662f\u56de\u5230\u4e86\u7ebf\u7a0b\u6c60\u3002<\/li>\n<li>\u6b64\u65f6\u8bf7\u6c42 B \u8fdb\u6765&#xff0c;\u521a\u597d\u590d\u7528\u4e86\u201c\u7ebf\u7a0b 1\u201d\u3002\u7531\u4e8e\u201c\u7ebf\u7a0b 1\u201d\u6ca1\u6709\u88ab\u91cd\u65b0 new&#xff0c;\u4e5f\u5c31\u4e0d\u4f1a\u91cd\u65b0\u89e6\u53d1\u62f7\u8d1d\u903b\u8f91\u3002\u7ed3\u679c\u5c31\u662f&#xff1a;\u8bf7\u6c42 B \u7684\u4e1a\u52a1\u4ee3\u7801\u91cc&#xff0c;\u6253\u5370\u51fa\u6765\u7684\u5c45\u7136\u662f\u8bf7\u6c42 A \u7684 TraceId&#xff01; \u8fd9\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u662f\u6781\u5176\u4e25\u91cd\u7684\u201c\u6570\u636e\u4e32\u4e71\u201d\u4e8b\u6545\u3002<\/li>\n<p>\u5982\u4f55\u7834\u5c40&#xff1f;<\/p>\n<p>\u4e1a\u754c\u5bf9\u4e8e\u7ebf\u7a0b\u6c60\u4e0a\u4e0b\u6587\u4f20\u9012\u7684\u6807\u51c6\u7b54\u6848\u662f&#xff1a;\u5f15\u5165\u963f\u91cc\u5df4\u5df4\u5f00\u6e90\u7684 **TransmittableThreadLocal** (\u7b80\u79f0 TTL)\u3002 TTL \u7684\u6838\u5fc3\u601d\u60f3\u662f\u5728\u5411\u7ebf\u7a0b\u6c60\u63d0\u4ea4\u4efb\u52a1\u7684\u90a3\u4e00\u523b\u8fdb\u884c\u4e0a\u4e0b\u6587\u7684\u6293\u53d6&#xff0c;\u5e76\u5728\u4efb\u52a1\u6267\u884c\u524d\u8fdb\u884c\u66ff\u6362&#xff0c;\u4efb\u52a1\u6267\u884c\u540e\u8fdb\u884c\u6062\u590d\u3002\u81f3\u4e8e TTL \u5e95\u5c42\u90a3\u4e9b\u7cbe\u5999\u7684\u5305\u88c5\u5668\u6a21\u5f0f&#xff08;Wrapper&#xff09;\u548c\u72b6\u6001\u673a\u6d41\u8f6c&#xff0c;\u6211\u4eec\u6709\u673a\u4f1a\u518d\u5c55\u5f00\u3002<\/p>\n<h4>\u4e94\u3001\u603b\u7ed3\u4e0e\u5c55\u671b&#xff1a;\u5e76\u53d1\u8bbe\u8ba1\u7684\u6700\u9ad8\u5883\u754c\u662f\u201c\u4e0d\u5e76\u53d1\u201d<\/h4>\n<p>\u5728\u65e5\u5e38\u7684\u4e1a\u52a1\u5f00\u53d1\u4e2d&#xff0c;\u5f88\u591a\u4eba\u4e00\u542c\u5230\u201c\u7ebf\u7a0b\u5b89\u5168\u201d&#xff0c;\u7b2c\u4e00\u53cd\u5e94\u5c31\u662f\u6761\u4ef6\u53cd\u5c04\u822c\u5730\u52a0\u4e0a synchronized \u6216 ReentrantLock\u3002\u4f46\u8bfb\u5b8c\u672c\u6587\u4e4b\u540e\u5e0c\u671b\u4f60\u80fd\u5efa\u7acb\u8d77\u4e00\u79cd\u67b6\u6784\u601d\u7ef4&#xff1a;\u5e76\u53d1\u63a7\u5236\u7684\u6700\u9ad8\u5883\u754c&#xff0c;\u5c31\u662f\u901a\u8fc7\u8bbe\u8ba1\u6765\u6d88\u706d\u5e76\u53d1\u7ade\u4e89\u3002<\/p>\n<p>\u65e0\u8bba\u662f Final \u5e26\u6765\u7684\u4e0d\u53ef\u53d8\u6027&#xff0c;\u8fd8\u662f ThreadLocal \u5e26\u6765\u7684\u7ebf\u7a0b\u7ea7\u9694\u79bb&#xff0c;\u5b83\u4eec\u672c\u8d28\u4e0a\u90fd\u5728\u5411\u6211\u4eec\u4f20\u8fbe\u4e00\u4e2a\u6838\u5fc3\u6307\u5bfc\u601d\u60f3&#xff1a;\u907f\u514d\u5171\u4eab\u53ef\u53d8\u72b6\u6001\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u6570\u636e\u5fc5\u987b\u5171\u4eab&#xff0c;\u90a3\u5c31\u8ba9\u5b83\u4e0d\u53ef\u53d8&#xff08;Final \/ \u4eab\u5143\u6a21\u5f0f&#xff09;\u3002<\/li>\n<li>\u5982\u679c\u6570\u636e\u5fc5\u987b\u53ef\u53d8&#xff0c;\u90a3\u5c31\u8ba9\u5b83\u4e0d\u5171\u4eab&#xff08;ThreadLocal \u526f\u672c\u673a\u5236&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u61c2\u5f97\u4e86\u8fd9\u4e9b&#xff0c;\u4f60\u5c31\u771f\u6b63\u8df3\u51fa\u4e86\u201c\u9762\u5411\u9501\u7f16\u7a0b\u201d\u7684\u4f4e\u7ea7\u9677\u9631&#xff0c;\u80fd\u591f\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u5199\u51fa\u6027\u80fd\u6781\u9ad8\u3001\u7edd\u5bf9\u5b89\u5168\u7684\u4f18\u96c5\u4ee3\u7801\u3002\u5f53\u7136&#xff0c;\u7edd\u6740\u7684\u62a4\u8eab\u7b26\u6c38\u8fdc\u662f\u4e25\u8c28\u7684\u4e1a\u52a1\u89c4\u8303&#xff08;\u6bd4\u5982 finally \u5757\u4e2d\u7684 remove()&#xff09;\u3002<\/p>\n<p>\u4e0b\u4e00\u7bc7\u9884\u544a&#xff1a;\u4e07\u6076\u4e4b\u6e90\u201c\u7ebf\u7a0b\u6c60\u201d\u7684\u5e95\u5c42\u54f2\u5b66<\/p>\n<p>\u5728\u63a2\u8ba8 ThreadLocal \u5185\u5b58\u6cc4\u6f0f\u5371\u673a\u4ee5\u53ca InheritableThreadLocal \u8de8\u7ebf\u7a0b\u4f20\u503c\u7684\u75db\u70b9\u65f6&#xff0c;\u6211\u4eec\u90fd\u4e0d\u7ea6\u800c\u540c\u5730\u649e\u4e0a\u4e86\u4e00\u5ea7\u5927\u5c71\u2014\u2014\u7ebf\u7a0b\u6c60\u3002<\/p>\n<p>\u51e0\u4e4e\u6240\u6709\u7684\u9ad8\u7ea7\u5e76\u53d1\u95ee\u9898&#xff0c;\u90fd\u4e0e\u7ebf\u7a0b\u6c60\u7684\u201c\u590d\u7528\u201d\u673a\u5236\u8131\u4e0d\u4e86\u5e72\u7cfb\u3002\u90a3\u4e48&#xff0c;\u5728\u73b0\u4ee3\u540e\u7aef\u67b6\u6784\u4e2d\u5904\u4e8e\u7edd\u5bf9\u6838\u5fc3\u5730\u4f4d\u7684 ThreadPoolExecutor \u5230\u5e95\u662f\u600e\u4e48\u8fd0\u8f6c\u7684&#xff1f;\u963f\u91cc\u300aJava \u5f00\u53d1\u624b\u518c\u300b\u4e3a\u4ec0\u4e48\u4e25\u7981\u4f7f\u7528 Executors \u521b\u5efa\u7ebf\u7a0b\u6c60&#xff0c;\u5b83\u5230\u5e95\u85cf\u7740\u4ec0\u4e48\u5bfc\u81f4 OOM \u7684\u60ca\u5929\u5927\u5751&#xff1f;\u5728\u771f\u5b9e\u7684\u751f\u4ea7\u73af\u5883\u4e2d&#xff0c;\u9762\u5bf9 CPU \u5bc6\u96c6\u578b\u548c I\/O \u5bc6\u96c6\u578b\u4efb\u52a1&#xff0c;\u6838\u5fc3\u7ebf\u7a0b\u6570\u5230\u5e95\u8be5\u600e\u4e48\u914d\u7f6e\u624d\u4e0d\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u96ea\u5d29&#xff1f;<br \/>\n\u8fd9\u4e00\u5207&#xff0c;\u5c06\u5728\u4e0b\u4e00\u7bc7 \u300a\u5e76\u53d1\u7f16\u7a0b\u6838\u5fc3&#xff1a;Java \u7ebf\u7a0b\u6c60\u67b6\u6784\u3001\u539f\u7406\u4e0e\u751f\u4ea7\u7ea7\u914d\u7f6e\u300b \u4e2d\u4e3a\u4f60\u5f7b\u5e95\u89e3\u5f00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00<br \/>\n\u5728\u591a\u7ebf\u7a0b\u7684\u4e16\u754c\u91cc&#xff0c;\u89e3\u51b3\u5e76\u53d1\u5b89\u5168\u95ee\u9898\u901a\u5e38\u6709\u4e24\u4e2a\u6781\u7aef\u601d\u8def&#xff1a;\u4e00\u4e2a\u662f\u201c\u5927\u5bb6\u6392\u961f\u7528\u201d&#xff0c;\u8fd9\u5c31\u662f\u52a0\u9501&#xff08;Synchronized\/ReentrantLock&#xff09;&#xff1b;\u53e6\u4e00\u4e2a\u5219\u662f\u201c\u5404\u7528\u5404\u7684\u201d\u6216\u8005\u201c\u8c01\u90fd\u522b\u60f3\u6539\u201d\u3002\u672c\u7bc7\u535a\u5ba2\u6211\u4eec\u5c06\u8df3\u51fa\u4f20\u7edf\u7684\u52a0\u9501\u601d\u7ef4&#xff0c;\u63a2\u8ba8\u5e76\u53d1\u7f16\u7a0b\u4e2d\u6781\u5176\u4f18\u96c5\u7684\u4e24\u79cd\u65e0\u9501\u54f2\u5b66&#xff1a;\u4e0d\u53ef\u53d8\u6027\u8bbe\u8ba1&#xff08;Final&#xff09;\u4e0e\u7ebf\u7a0b\u7ea7\u9694\u79bb&#xff08;ThreadLocal<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,426,190,188],"topic":[],"class_list":["post-79957","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-jvm","tag-190","tag-188"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u300cJUC\u300d\u7ebf\u7a0b\u9694\u79bb\u4e0e\u4e0d\u53ef\u53d8\u6027\uff1aThreadLocal \u6e90\u7801\u5256\u6790\u4e0e Final \u5173\u952e\u5b57 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/79957.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u300cJUC\u300d\u7ebf\u7a0b\u9694\u79bb\u4e0e\u4e0d\u53ef\u53d8\u6027\uff1aThreadLocal \u6e90\u7801\u5256\u6790\u4e0e Final \u5173\u952e\u5b57 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u5f15\u8a00 \u5728\u591a\u7ebf\u7a0b\u7684\u4e16\u754c\u91cc&#xff0c;\u89e3\u51b3\u5e76\u53d1\u5b89\u5168\u95ee\u9898\u901a\u5e38\u6709\u4e24\u4e2a\u6781\u7aef\u601d\u8def&#xff1a;\u4e00\u4e2a\u662f\u201c\u5927\u5bb6\u6392\u961f\u7528\u201d&#xff0c;\u8fd9\u5c31\u662f\u52a0\u9501&#xff08;Synchronized\/ReentrantLock&#xff09;&#xff1b;\u53e6\u4e00\u4e2a\u5219\u662f\u201c\u5404\u7528\u5404\u7684\u201d\u6216\u8005\u201c\u8c01\u90fd\u522b\u60f3\u6539\u201d\u3002\u672c\u7bc7\u535a\u5ba2\u6211\u4eec\u5c06\u8df3\u51fa\u4f20\u7edf\u7684\u52a0\u9501\u601d\u7ef4&#xff0c;\u63a2\u8ba8\u5e76\u53d1\u7f16\u7a0b\u4e2d\u6781\u5176\u4f18\u96c5\u7684\u4e24\u79cd\u65e0\u9501\u54f2\u5b66&#xff1a;\u4e0d\u53ef\u53d8\u6027\u8bbe\u8ba1&#xff08;Final&#xff09;\u4e0e\u7ebf\u7a0b\u7ea7\u9694\u79bb&#xff08;ThreadLocal\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/79957.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-03-03T12:58:06+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79957.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/79957.html\",\"name\":\"\u300cJUC\u300d\u7ebf\u7a0b\u9694\u79bb\u4e0e\u4e0d\u53ef\u53d8\u6027\uff1aThreadLocal \u6e90\u7801\u5256\u6790\u4e0e Final \u5173\u952e\u5b57 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-03-03T12:58:06+00:00\",\"dateModified\":\"2026-03-03T12:58:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/79957.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/79957.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/79957.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u300cJUC\u300d\u7ebf\u7a0b\u9694\u79bb\u4e0e\u4e0d\u53ef\u53d8\u6027\uff1aThreadLocal \u6e90\u7801\u5256\u6790\u4e0e Final \u5173\u952e\u5b57\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u300cJUC\u300d\u7ebf\u7a0b\u9694\u79bb\u4e0e\u4e0d\u53ef\u53d8\u6027\uff1aThreadLocal \u6e90\u7801\u5256\u6790\u4e0e Final \u5173\u952e\u5b57 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/79957.html","og_locale":"zh_CN","og_type":"article","og_title":"\u300cJUC\u300d\u7ebf\u7a0b\u9694\u79bb\u4e0e\u4e0d\u53ef\u53d8\u6027\uff1aThreadLocal \u6e90\u7801\u5256\u6790\u4e0e Final \u5173\u952e\u5b57 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u5f15\u8a00 \u5728\u591a\u7ebf\u7a0b\u7684\u4e16\u754c\u91cc&#xff0c;\u89e3\u51b3\u5e76\u53d1\u5b89\u5168\u95ee\u9898\u901a\u5e38\u6709\u4e24\u4e2a\u6781\u7aef\u601d\u8def&#xff1a;\u4e00\u4e2a\u662f\u201c\u5927\u5bb6\u6392\u961f\u7528\u201d&#xff0c;\u8fd9\u5c31\u662f\u52a0\u9501&#xff08;Synchronized\/ReentrantLock&#xff09;&#xff1b;\u53e6\u4e00\u4e2a\u5219\u662f\u201c\u5404\u7528\u5404\u7684\u201d\u6216\u8005\u201c\u8c01\u90fd\u522b\u60f3\u6539\u201d\u3002\u672c\u7bc7\u535a\u5ba2\u6211\u4eec\u5c06\u8df3\u51fa\u4f20\u7edf\u7684\u52a0\u9501\u601d\u7ef4&#xff0c;\u63a2\u8ba8\u5e76\u53d1\u7f16\u7a0b\u4e2d\u6781\u5176\u4f18\u96c5\u7684\u4e24\u79cd\u65e0\u9501\u54f2\u5b66&#xff1a;\u4e0d\u53ef\u53d8\u6027\u8bbe\u8ba1&#xff08;Final&#xff09;\u4e0e\u7ebf\u7a0b\u7ea7\u9694\u79bb&#xff08;ThreadLocal","og_url":"https:\/\/www.wsisp.com\/helps\/79957.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-03-03T12:58:06+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"8 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/79957.html","url":"https:\/\/www.wsisp.com\/helps\/79957.html","name":"\u300cJUC\u300d\u7ebf\u7a0b\u9694\u79bb\u4e0e\u4e0d\u53ef\u53d8\u6027\uff1aThreadLocal \u6e90\u7801\u5256\u6790\u4e0e Final \u5173\u952e\u5b57 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-03-03T12:58:06+00:00","dateModified":"2026-03-03T12:58:06+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/79957.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/79957.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/79957.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u300cJUC\u300d\u7ebf\u7a0b\u9694\u79bb\u4e0e\u4e0d\u53ef\u53d8\u6027\uff1aThreadLocal \u6e90\u7801\u5256\u6790\u4e0e Final \u5173\u952e\u5b57"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=79957"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/79957\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=79957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=79957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=79957"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=79957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}