{"id":16420,"date":"2024-10-09T20:52:41","date_gmt":"2024-10-09T12:52:41","guid":{"rendered":"https:\/\/92it.top\/?p=16420"},"modified":"2024-10-09T20:52:41","modified_gmt":"2024-10-09T12:52:41","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88threadlocal%ef%bc%8cremove%e4%b9%8b%e5%90%8e%e4%bb%8d%e7%84%b6%e6%9c%89%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16420","title":{"rendered":"\u4e3a\u4ec0\u4e48ThreadLocal\uff0cRemove\u4e4b\u540e\u4ecd\u7136\u6709\u503c\uff1f"},"content":{"rendered":"\n<p>\u4e3a\u4ec0\u4e48\u662f123\uff0c\u4e0d\u5e94\u8be5\u662fnull\u5417\uff1f\u901a\u4e49\u5634\u5f88\u786c\uff0c\u4e00\u53e3\u54ac\u5b9a\u662fnull<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"404\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image-1024x404.png\" alt=\"\" class=\"wp-image-16421\" style=\"width:582px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image-1024x404.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image-300x118.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image-768x303.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image-1536x606.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image-830x328.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image-230x91.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image-350x138.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image-480x189.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/10\/image.png 1556w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p>\u5728Java\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c<code>ThreadLocal<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u521b\u5efa\u72ec\u7acb\u7684\u53d8\u91cf\u526f\u672c\u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u5b83\u7684\u884c\u4e3a\u53ef\u80fd\u4f1a\u8ba9\u4eba\u611f\u5230\u56f0\u60d1\u3002\u4f8b\u5982\uff0c\u4e0a\u8ff0\u4ee3\u7801\u5728\u8c03\u7528<code>local.remove()<\/code>\u4e4b\u540e\uff0c\u8c03\u7528<code>local.get()<\/code>\u4f9d\u7136\u8fd4\u56de123\uff0c\u800c\u4e0d\u662f\u6211\u4eec\u9884\u671f\u7684null\u3002\u8fd9\u5230\u5e95\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u672c\u6587\u5c06\u6df1\u5165\u5206\u6790\u8fd9\u4e00\u73b0\u8c61\u7684\u539f\u56e0\uff0c\u5e76\u901a\u8fc7\u8be6\u7ec6\u7684\u4ee3\u7801\u793a\u4f8b\u8fdb\u884c\u89e3\u91ca\u3002<\/p>\n\n\n\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n\n\n\n<p>\u8ba9\u6211\u4eec\u9996\u5148\u770b\u4e00\u4e0b\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class ThreadLocalExample {\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2aThreadLocal\u53d8\u91cf\n\n\uff0c\u5e76\u8bbe\u7f6e\u521d\u59cb\u503c\u4e3a123\n        ThreadLocal&lt;Integer> local = ThreadLocal.withInitial(() -> 123);\n        \n        \/\/ \u8bbe\u7f6eThreadLocal\u53d8\u91cf\u7684\u503c\u4e3a1\n        local.set(1);\n        \n        \/\/ \u79fb\u9664\u5f53\u524d\u7ebf\u7a0b\u7684ThreadLocal\u53d8\u91cf\u503c\n        local.remove();\n        \n        \/\/ \u8f93\u51fa\u5f53\u524d\u7ebf\u7a0b\u7684ThreadLocal\u53d8\u91cf\u503c\n        System.out.println(local.get());\n    }\n}<\/pre>\n\n\n\n<p>\u5728\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u662f<code>123<\/code>\uff0c\u800c\u4e0d\u662f<code>null<\/code>\u3002\u8fd9\u8bf4\u660e\u5373\u4f7f\u6211\u4eec\u8c03\u7528\u4e86<code>remove<\/code>\u65b9\u6cd5\uff0c<code>ThreadLocal<\/code>\u53d8\u91cf\u4ecd\u7136\u8fd4\u56de\u4e86\u521d\u59cb\u503c\u3002\u8fd9\u80cc\u540e\u6d89\u53ca\u5230<code>ThreadLocal<\/code>\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n\n\n\n<p><strong><code>ThreadLocal<\/code>\u7684\u5de5\u4f5c\u539f\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e3a\u4e86\u7406\u89e3\u8fd9\u4e00\u73b0\u8c61\uff0c\u6211\u4eec\u9700\u8981\u6df1\u5165\u4e86\u89e3<code>ThreadLocal<\/code>\u7684\u5de5\u4f5c\u539f\u7406\u3002<code>ThreadLocal<\/code>\u5b9e\u9645\u4e0a\u662f\u901a\u8fc7\u4e00\u4e2a\u5185\u90e8\u7684<code>ThreadLocalMap<\/code>\u6765\u5b58\u50a8\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u53d8\u91cf\u526f\u672c\u3002<\/p>\n\n\n\n<p><strong><code>ThreadLocal.withInitial<\/code><\/strong>: \u5f53\u6211\u4eec\u4f7f\u7528<code>ThreadLocal.withInitial<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a<code>ThreadLocal<\/code>\u53d8\u91cf\u65f6\uff0c\u6211\u4eec\u4f20\u5165\u4e86\u4e00\u4e2a<code>Supplier<\/code>\u51fd\u6570\uff0c\u7528\u6765\u63d0\u4f9b\u8be5\u53d8\u91cf\u7684\u521d\u59cb\u503c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u521d\u59cb\u503c\u4e3a<code>123<\/code>\u3002<\/p>\n\n\n\n<p><strong><code>local.set(1)<\/code><\/strong>: \u8c03\u7528<code>local.set(1)<\/code>\u65b9\u6cd5\uff0c\u5c06\u5f53\u524d\u7ebf\u7a0b\u7684<code>ThreadLocal<\/code>\u53d8\u91cf\u503c\u8bbe\u7f6e\u4e3a<code>1<\/code>\u3002<\/p>\n\n\n\n<p><strong><code>local.remove()<\/code><\/strong>: \u8c03\u7528<code>local.remove()<\/code>\u65b9\u6cd5\uff0c\u4f1a\u4ece\u5f53\u524d\u7ebf\u7a0b\u7684<code>ThreadLocalMap<\/code>\u4e2d\u79fb\u9664\u8fd9\u4e2a<code>ThreadLocal<\/code>\u53d8\u91cf\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u8fd9\u4e2a\u53d8\u91cf\u7684\u521d\u59cb\u503c\u88ab\u6e05\u9664\uff0c\u800c\u53ea\u662f\u79fb\u9664\u4e86\u5f53\u524d\u7ebf\u7a0b\u5bf9\u6b64\u53d8\u91cf\u7684\u663e\u5f0f\u8bbe\u7f6e\u503c\u3002<\/p>\n\n\n\n<p><strong><code>local.get()<\/code><\/strong>: \u5f53\u6211\u4eec\u8c03\u7528<code>local.get()<\/code>\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u7684<code>ThreadLocalMap<\/code>\u4e2d\u6ca1\u6709\u6b64\u53d8\u91cf\u7684\u503c\uff0c<code>ThreadLocal<\/code>\u4f1a\u8c03\u7528<code>initialValue<\/code>\u65b9\u6cd5\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u521d\u59cb\u503c\u3002\u5728\u672c\u4f8b\u4e2d\uff0c<code>initialValue<\/code>\u65b9\u6cd5\u8fd4\u56de<code>123<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u8be6\u7ec6\u4ee3\u7801\u89e3\u6790<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8ba9\u6211\u4eec\u9010\u884c\u89e3\u6790\u4e0a\u8ff0\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class ThreadLocalExample {\n    public static void main(String[] args) {\n        \/\/ \u521b\u5efa\u4e00\u4e2aThreadLocal\u53d8\u91cf\uff0c\u5e76\u8bbe\u7f6e\u521d\u59cb\u503c\u4e3a123\n        ThreadLocal&lt;Integer> local = ThreadLocal.withInitial(() -> 123);\n        \n        \/\/ \u8bbe\u7f6eThreadLocal\u53d8\u91cf\u7684\u503c\u4e3a1\n        local.set(1);\n        \/\/ \u6b64\u65f6\uff0c\u5f53\u524d\u7ebf\u7a0b\u7684ThreadLocalMap\u4e2d\u4fdd\u5b58\u4e86\u53d8\u91cflocal\u7684\u503c\u4e3a1\n        \n        \/\/ \u79fb\u9664\u5f53\u524d\u7ebf\u7a0b\u7684ThreadLocal\u53d8\u91cf\u503c\n        local.remove();\n        \/\/ remove\u65b9\u6cd5\u4eceThreadLocalMap\u4e2d\u79fb\u9664\u4e86\u53d8\u91cflocal\uff0c\u4f46\u521d\u59cb\u503c\u7684\u751f\u6210\u903b\u8f91\u4ecd\u7136\u5b58\u5728\n        \n        \/\/ \u8f93\u51fa\u5f53\u524d\u7ebf\u7a0b\u7684ThreadLocal\u53d8\u91cf\u503c\n        System.out.println(local.get());\n        \/\/ \u8c03\u7528get\u65b9\u6cd5\u65f6\uff0cThreadLocalMap\u4e2d\u6ca1\u6709\u53d8\u91cflocal\u7684\u663e\u5f0f\u503c\n        \/\/ \u56e0\u6b64ThreadLocal\u8c03\u7528initialValue\u65b9\u6cd5\uff0c\u8fd4\u56de\u521d\u59cb\u503c123\n    }\n}<\/pre>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u8be6\u7ec6\u89e3\u6790\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c<code>remove<\/code>\u65b9\u6cd5\u5e76\u4e0d\u4f1a\u6e05\u9664<code>ThreadLocal<\/code>\u53d8\u91cf\u7684\u521d\u59cb\u503c\u751f\u6210\u903b\u8f91\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u518d\u6b21\u8c03\u7528<code>get<\/code>\u65b9\u6cd5\u65f6\uff0c<code>ThreadLocal<\/code>\u4f1a\u91cd\u65b0\u8c03\u7528<code>initialValue<\/code>\u65b9\u6cd5\uff0c\u8fd4\u56de\u521d\u59cb\u503c<code>123<\/code>\u3002<\/p>\n\n\n\n<p><strong><code>ThreadLocal<\/code>\u4f7f\u7528\u5efa\u8bae<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e3a\u4e86\u907f\u514d<code>ThreadLocal<\/code>\u5e26\u6765\u7684\u6f5c\u5728\u95ee\u9898\uff0c\u6211\u4eec\u5728\u4f7f\u7528<code>ThreadLocal<\/code>\u65f6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<p><strong>\u53ca\u65f6\u6e05\u7406<\/strong>\uff1a\u5728\u4f7f\u7528\u5b8c<code>ThreadLocal<\/code>\u53d8\u91cf\u540e\uff0c\u5e94\u8be5\u8c03\u7528<code>remove<\/code>\u65b9\u6cd5\u53ca\u65f6\u6e05\u7406\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f.<\/p>\n\n\n\n<p><strong>\u6b63\u786e\u7406\u89e3\u521d\u59cb\u503c<\/strong>\uff1a\u7406\u89e3<code>ThreadLocal<\/code>\u7684\u521d\u59cb\u503c\u751f\u6210\u903b\u8f91\uff0c\u4e0d\u8981\u8bef\u4ee5\u4e3a\u8c03\u7528<code>remove<\/code>\u65b9\u6cd5\u4f1a\u6e05\u9664\u521d\u59cb\u503c\u3002<\/p>\n\n\n\n<p><strong>\u4f7f\u7528\u573a\u666f<\/strong>\uff1a\u5408\u7406\u4f7f\u7528<code>ThreadLocal<\/code>\uff0c\u907f\u514d\u6ee5\u7528\u3002\u5b83\u4e3b\u8981\u9002\u7528\u4e8e\u9700\u8981\u5728\u7ebf\u7a0b\u4e2d\u5b58\u50a8\u5168\u5c40\u72b6\u6001\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u8fde\u63a5\u3001\u4e8b\u52a1\u7ba1\u7406\u7b49\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff0\u5206\u6790\uff0c\u6211\u4eec\u8be6\u7ec6\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728\u8c03\u7528<code>ThreadLocal.remove<\/code>\u65b9\u6cd5\u4e4b\u540e\uff0c\u8c03\u7528<code>get<\/code>\u65b9\u6cd5\u4f9d\u7136\u8fd4\u56de\u521d\u59cb\u503c<code>123<\/code>\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u6df1\u5165\u89e3\u6790\uff0c\u80fd\u591f\u5e2e\u52a9\u5927\u5bb6\u66f4\u597d\u5730\u7406\u89e3<code>ThreadLocal<\/code>\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u5e76\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u6b63\u786e\u4f7f\u7528\u5b83\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u662f123\uff0c\u4e0d\u5e94\u8be5\u662fnull\u5417\uff1f\u901a\u4e49\u5634\u5f88\u786c\uff0c\u4e00\u53e3\u54ac\u5b9a\u662fnull \u5728Java\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0cThreadLoca [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16420"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16420"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16420\/revisions"}],"predecessor-version":[{"id":16423,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16420\/revisions\/16423"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}