{"id":10163,"date":"2023-11-28T21:27:27","date_gmt":"2023-11-28T13:27:27","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10163"},"modified":"2023-11-28T21:27:58","modified_gmt":"2023-11-28T13:27:58","slug":"java-zhongthreadlocal-dao-di-zhang-shen-me-you-she","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/java-zhongthreadlocal-dao-di-zhang-shen-me-you-she\/","title":{"rendered":"Java\u4e2dThreadLocal\u5230\u5e95\u9577\u4ec0\u4e48\uff1f\u6709\u4ec0\u4e48\u7528\uff1f"},"content":{"rendered":"<p>ThreadLocal\u901a\u8fc7\u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230\u7ebf\u7a0b\u4e0a\u4fdd\u8bc1\u4e86\u6570\u636e\u9694\u79bb\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u5b89\u5168\uff0c\u4f7f\u7528\u573a\u666f\u5927\u591a \u662f\u7ef4\u62a4\u4e00\u4e2a\u7ebf\u7a0b\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61\u3002<br \/>\nThreadLocal\u91cc\u5b58\u50a8\u7684\u6570\u636e\u672c\u8d28\u4e0a\u5c31\u5b58\u50a8\u5728Thread\u7c7b\u4e0b\u7684\u4e00\u4e2aMap\u91cc\uff0c\u4e5f\u662f\u7ebf\u7a0b\u7c7b\u7684\u6210\u5458\u53d8 \u91cf\uff0c\u548c\u4f60\u7684\u60f3\u6cd5\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u7c7b\u6210\u5458\u57df\u5176\u5b9e\u5dee\u4e0d\u591a\u3002<br \/>\n\u6b63\u786e\u7684\u4f7f\u7528\u59ff\u52bf\uff0c\u5c31\u662f\u7528\u6765\u5b9a\u4e49\u7ebf\u7a0b\u7ea7\u522b\u7684\u5168\u5c40\u53d8\u91cf\u7684\u3002<br \/>\n\u5168\u5c40\u53d8\u91cf\u662f\u6307\uff0c\u5728\u4efb\u610f\u7c7b\u4efb\u610f\u65b9\u6cd5\u4e2d\u90fd\u53ef\u4ee5\u8bfb\u53d6\u548c\u8bbe\u7f6e;\u7ebf\u7a0b\u7ea7\u610f\u601d\u5c31\u662f\u201c\u7ebf\u7a0b\u526f\u672c\u201d\uff0c\u8fd9 \u4e2a\u53d8\u91cf\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u4f46\u5728\u7ebf\u7a0b\u5185\u4e00\u5b9a\u662f\u552f\u4e00\u7684\u3002<\/p>\n<p>Thread\u7c7b\u4e2d\u6709\u4e00\u4e2a\u6210\u5458\u53d8\u91cfthreadLocals ,\u5b9e\u9645\u4e0a\uff0c\u5b83\u5c31\u662f\u4e00\u4e2ahashtable (map)\u3002\u4e5f\u5c31<br \/>\n\u662f\u5b58\u653e\u6240\u8c13\u201c\u7ebf\u7a0b\u526f\u672c\u201d\u7684\u5730\u65b9\u3002Java\u4e2d\u8bf4\u4e00\u4e0brunnable\u548ccallable\u6709\u4ec0\u4e48\u533a\u522b<\/p>\n<pre><code class=\"language-public\" data-meta=\"class Thread implements Runnable {\">\/*ThreadLocal values pertaining to this thread.This map is maintained\n* by the ThreadLocal class.*\/\nThreadLocal.ThreadLocalMap threadLocals = null;\n<\/code><\/pre>\n<p>\u800cThreadLocal\u5b9e\u9645\u5c31\u662f\u64cd\u4f5c\u8fd9\u4e2ahash table\u4e2d\u56fa\u5b9a\u4e00\u884c\u8bb0\u5f55\u7684\u5feb\u6377\u65b9\u5f0f\u800c\u5df2\u3002<br \/>\n\u6211\u4eec\u6362\u4e2alow \u2014\u70b9\u7684\u4ee3\u7801\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd<\/p>\n<pre><code class=\"language-public\" data-meta=\"class Thread implements Runnable {\">private Map&lt;Object,Object&gt; threadLocals = new HashMap&lt;&gt;();\npublic void setLocal(Object key,Object val) {\nthreadLocals.put(key,val);\n}\npublic void getLocal(Object key) {\nreturn threadLocals.get(key);\n}\n\/\/ ....\u7701\u7565n\u591athread\u7684\u4ee3\u7801\n\u6211\u4eec\u518d\u5b9a\u4e49\u4e00\u7ec4key,\u5176\u5b9e\u5c31\u662f\u7ebf\u7a0b\u7ea7\u5168\u5c40\u53d8\u91cf\u540d\u3002\u663e\u7136\uff0c\u5168\u5c40\u53d8\u91cf\u540d\u9700\u8981\u5728\u4efb\u610f\u4ee3\u7801\u5904\u90fd\u80fd \u8bbf\u95ee\u5230\uff0c\u56e0\u6b64\u6211\u4eec\u58f0\u660e\u4e3apublic static\u7684\u3002\npublic class ThreadLocalKey {\n\/*\u8bf7\u6c42id *\/\npublic static final String TRACE_ID = &quot;traceld&quot;;\n\/*\u538b\u6d4b\u6807*\/\npublic static final String STRESS_FLAG = &quot;stressFlag&quot;;\n}\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u4efb\u610f\u7c7b\u7684\u4efb\u610f\u4ee3\u7801\u5904\u8bbe\u7f6e\u548c\u8bfb\u53d6\u8fd9\u4e2a\u201c\u7ebf\u7a0b\u7ea7\u5168\u5c40\u53d8\u91cf\u201d \u4e86\u3002<br \/>\n\/*\u4e00\u5904\u8bbe\u7f6e traceld *\/<br \/>\nThread.currentThread().setLocal (ThreadLocalKey.TRACE_ID,UUID.randomUUID().toString());<br \/>\n\/<em>\u53e6\u4e00\u5904\u8bfb\u53d6traceld,\u4e0d\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u8c03\u7528<\/em>\/<br \/>\nThread.currentThread().getLocal (ThreadLocalKey.TRACE_ID);<br \/>\n\u800cJDK\u63d0\u4f9b\u7684ThreadLocal,\u5b9e\u9645\u5145\u5f53\u7684\u5c31\u662f\u4e0a\u9762\u8fd9\u4e2aThreadLocalKey.TRACE_ID\u8fd9\u4e2ahash table key\u7684\u4f5c\u7528\u3002JDK\u5b9e\u73b0\u6ca1\u6709\u8fd9\u4e48\u7c97\u66b4\uff0c\u5b83\u628aThread\u4e2d\u7684hash table\u7ed9\u9690\u85cf\u8d77\u6765\u4e86\uff0c\u4e0d \u76f4\u63a5\u7ed9\u5916\u90e8\u8bbf\u95ee\u3002\u56e0\u6b64\uff0c\u4f7f\u7528ThreadLocal\u65f6\uff0c\u4e00\u822c\u4e5f\u662f\u58f0\u660e\u4e3astatic,\u6216\u8005\u80fd\u88ab\u9759\u6001\u8bbf\u95ee \u5230\u3002<br \/>\n\u90a3\u7528ThreadLocal\u5b9e\u73b0\u7c7b\u4f3c\u7684\u5168\u5c40\u5b58\u50a8traceId\u7684\u529f\u80fd\uff0c\u4e00\u822c\u5c31\u662f\u4e0b\u9762\u8fd9\u6837<\/p>\n<pre><code class=\"language-public\" data-meta=\"class RequestContext {\">private static final ThreadLocal&lt;String&gt; traceldLocal = new ThreadLocal&lt;&gt;();\npublic static String getTraceId() {\nreturn threadldLocal.get();\npublic static String setTraceld(String traceId) {\nthreadldLocal.set(traceld);\n}\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u5728\u4efb\u610f\u4ee3\u7801\u4f4d\u7f6e\u901a\u8fc7ThreadLocal\u6765\u8bbf\u95ee\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf<br \/>\n\/* \u2014\u5904\u8bbe\u7f6e traceld *\/<br \/>\nRequestContext.setTraceld (UUID.randomUUID().toString());<br \/>\n\/<em>\u53e6\u4e00\u5904\u8bfb\u53d6traceld,\u4e0d\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\u8c03\u7528<\/em>\/<br \/>\nRequestContext.getTraceId() ;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ThreadLocal\u901a\u8fc7\u5c06\u53d8\u91cf\u7ed1\u5b9a\u5230\u7ebf\u7a0b\u4e0a\u4fdd\u8bc1\u4e86\u6570\u636e\u9694\u79bb\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u5b89\u5168\uff0c\u4f7f\u7528\u573a\u666f\u5927\u591a \u662f\u7ef4\u62a4\u4e00\u4e2a\u7ebf\u7a0b\u7684 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118],"tags":[],"class_list":["post-10163","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10163","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=10163"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10163\/revisions"}],"predecessor-version":[{"id":10164,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10163\/revisions\/10164"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10163"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}