{"id":406,"date":"2024-10-22T21:08:12","date_gmt":"2024-10-22T13:08:12","guid":{"rendered":"https:\/\/www.zackzzp.top\/?p=406"},"modified":"2024-10-22T21:08:39","modified_gmt":"2024-10-22T13:08:39","slug":"%e4%b8%80%e6%96%87%e4%ba%86%e8%a7%a3threadlocal","status":"publish","type":"post","link":"http:\/\/www.zackzzp.top\/index.php\/2024\/10\/22\/%e4%b8%80%e6%96%87%e4%ba%86%e8%a7%a3threadlocal\/","title":{"rendered":"\u4e00\u6587\u4e86\u89e3ThreadLocal"},"content":{"rendered":"<h2><br \/>\u7b80\u4ecb<\/h2>\n<p>ThreadLocal\u53eb\u505a\u7ebf\u7a0b\u53d8\u91cf\uff0c\u610f\u601d\u662fThreadLocal\u4e2d\u586b\u5145\u7684\u53d8\u91cf\u5c5e\u4e8e\u5f53\u524d\u7ebf\u7a0b\uff0c\u8be5\u53d8\u91cf\u5bf9\u5176\u4ed6\u7ebf\u7a0b\u800c\u8a00\u662f\u9694\u79bb\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u8be5\u53d8\u91cf\u662f\u5f53\u524d\u7ebf\u7a0b\u72ec\u6709\u7684\u53d8\u91cf\u3002ThreadLocal\u4e3a\u53d8\u91cf\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u90fd\u521b\u5efa\u4e86\u4e00\u4e2a\u526f\u672c\uff0c\u90a3\u4e48\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u81ea\u5df1\u5185\u90e8\u7684\u526f\u672c\u53d8\u91cf\u3002<\/p>\n<p><strong>ThreadLoal \u53d8\u91cf\uff0c\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf<\/strong>\uff0c\u540c\u4e00\u4e2a ThreadLocal \u6240\u5305\u542b\u7684\u5bf9\u8c61\uff0c\u5728\u4e0d\u540c\u7684 Thread \u4e2d\u6709\u4e0d\u540c\u7684\u526f\u672c\u3002\u8fd9\u91cc\u6709\u51e0\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<p>\u56e0\u4e3a\u6bcf\u4e2a Thread \u5185\u6709\u81ea\u5df1\u7684\u5b9e\u4f8b\u526f\u672c\uff0c\u4e14\u8be5\u526f\u672c\u53ea\u80fd\u7531\u5f53\u524d Thread \u4f7f\u7528\u3002\u8fd9\u662f\u4e5f\u662f ThreadLocal \u547d\u540d\u7684\u7531\u6765\u3002<br \/>\u65e2\u7136\u6bcf\u4e2a Thread \u6709\u81ea\u5df1\u7684\u5b9e\u4f8b\u526f\u672c\uff0c\u4e14\u5176\u5b83 Thread \u4e0d\u53ef\u8bbf\u95ee\uff0c\u90a3\u5c31\u4e0d\u5b58\u5728\u591a\u7ebf\u7a0b\u95f4\u5171\u4eab\u7684\u95ee\u9898\u3002<br \/>ThreadLocal \u63d0\u4f9b\u4e86\u7ebf\u7a0b\u672c\u5730\u7684\u5b9e\u4f8b\u3002\u5b83\u4e0e\u666e\u901a\u53d8\u91cf\u7684\u533a\u522b\u5728\u4e8e\uff0c\u6bcf\u4e2a\u4f7f\u7528\u8be5\u53d8\u91cf\u7684\u7ebf\u7a0b\u90fd\u4f1a\u521d\u59cb\u5316\u4e00\u4e2a\u5b8c\u5168\u72ec\u7acb\u7684\u5b9e\u4f8b\u526f\u672c\u3002ThreadLocal \u53d8\u91cf\u901a\u5e38\u88abprivate static\u4fee\u9970\u3002\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u7ed3\u675f\u65f6\uff0c\u5b83\u6240\u4f7f\u7528\u7684\u6240\u6709 ThreadLocal \u76f8\u5bf9\u7684\u5b9e\u4f8b\u526f\u672c\u90fd\u53ef\u88ab\u56de\u6536\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0cThreadLocal \u9002\u7528\u4e8e\u6bcf\u4e2a\u7ebf\u7a0b\u9700\u8981\u81ea\u5df1\u72ec\u7acb\u7684\u5b9e\u4f8b\u4e14\u8be5\u5b9e\u4f8b\u9700\u8981\u5728\u591a\u4e2a\u65b9\u6cd5\u4e2d\u88ab\u4f7f\u7528\uff0c\u4e5f\u5373\u53d8\u91cf\u5728\u7ebf\u7a0b\u95f4\u9694\u79bb\u800c\u5728\u65b9\u6cd5\u6216\u7c7b\u95f4\u5171\u4eab\u7684\u573a\u666f<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.picgo.net\/2024\/10\/22\/imagee35912e17030dd6e.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.picgo.net\/2024\/10\/22\/imagee35912e17030dd6e.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image\" border=\"0\" \/><\/div><\/p>\n<hr \/>\n<h2>ThreadLocal\u548cSychnorized\u7684\u533a\u522b<\/h2>\n<p>ThreadLocal&lt;T&gt;\u5176\u5b9e\u662f\u4e0e\u7ebf\u7a0b\u7ed1\u5b9a\u7684\u4e00\u4e2a\u53d8\u91cf\u3002ThreadLocal\u548cSynchonized\u90fd\u7528\u4e8e\u89e3\u51b3\u591a\u7ebf\u7a0b\u5e76\u53d1\u8bbf\u95ee\u3002<\/p>\n<p>\u4f46\u662fThreadLocal\u4e0esynchronized\u6709\u672c\u8d28\u7684\u533a\u522b\uff1a<\/p>\n<p>1\u3001Synchronized\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7684\u6570\u636e\u5171\u4eab\uff0c\u800cThreadLocal\u5219\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7684\u6570\u636e\u9694\u79bb\u3002<\/p>\n<p>2\u3001Synchronized\u662f\u5229\u7528\u9501\u7684\u673a\u5236\uff0c\u4f7f\u53d8\u91cf\u6216\u4ee3\u7801\u5757\u5728\u67d0\u4e00\u65f6\u8be5\u53ea\u80fd\u88ab\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u3002\u800cThreadLocal\u4e3a\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u63d0\u4f9b\u4e86\u53d8\u91cf\u7684\u526f\u672c\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u67d0\u4e00\u65f6\u95f4\u8bbf\u95ee\u5230\u7684\u5e76\u4e0d\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u6837\u5c31\u9694\u79bb\u4e86\u591a\u4e2a\u7ebf\u7a0b\u5bf9\u6570\u636e\u7684\u6570\u636e\u5171\u4eab\u3002\u800cSynchronized\u5374\u6b63\u597d\u76f8\u53cd\uff0c\u5b83\u7528\u4e8e\u5728\u591a\u4e2a\u7ebf\u7a0b\u95f4\u901a\u4fe1\u65f6\u80fd\u591f\u83b7\u5f97\u6570\u636e\u5171\u4eab\u3002<\/p>\n<p>\u4e00\u53e5\u8bdd\u7406\u89e3ThreadLocal\uff0cthreadlocl\u662f\u4f5c\u4e3a\u5f53\u524d\u7ebf\u7a0b\u4e2d\u5c5e\u6027ThreadLocalMap\u96c6\u5408\u4e2d\u7684\u67d0\u4e00\u4e2aEntry\u7684key\u503cEntry\uff08threadlocl,value\uff09\uff0c\u867d\u7136\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e4b\u95f4threadlocal\u8fd9\u4e2akey\u503c\u662f\u4e00\u6837\uff0c\u4f46\u662f\u4e0d\u540c\u7684\u7ebf\u7a0b\u6240\u62e5\u6709\u7684ThreadLocalMap\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\uff0c\u4e5f\u5c31\u662f\u4e0d\u540c\u7684\u7ebf\u7a0b\u95f4\u540c\u4e00\u4e2aThreadLocal\uff08key\uff09\u5bf9\u5e94\u5b58\u50a8\u7684\u503c(value)\u4e0d\u4e00\u6837\uff0c\u4ece\u800c\u5230\u8fbe\u4e86\u7ebf\u7a0b\u95f4\u53d8\u91cf\u9694\u79bb\u7684\u76ee\u7684\uff0c\u4f46\u662f\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u8fd9\u4e2avalue\u53d8\u91cf\u5730\u5740\u662f\u4e00\u6837\u7684\u3002<\/p>\n<hr \/>\n<h2>ThreadLocal\uff0cThread\u548cThreadLocalMap\u7684\u5173\u7cfb<\/h2>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.picgo.net\/2024\/10\/22\/image4cb902be10617e00.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.picgo.net\/2024\/10\/22\/image4cb902be10617e00.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image\" border=\"0\" \/><\/div><\/p>\n<hr \/>\n<h2>ThrealLocal\u5e94\u7528\u573a\u666f<\/h2>\n<p>ThreadLocal \u9002\u7528\u4e8e\u5982\u4e0b\u4e24\u79cd\u573a\u666f<\/p>\n<ul>\n<li><strong><em>1\u3001\u6bcf\u4e2a\u7ebf\u7a0b\u9700\u8981\u6709\u81ea\u5df1\u5355\u72ec\u7684\u5b9e\u4f8b<\/em><\/strong><\/li>\n<li><strong><em>2\u3001\u5b9e\u4f8b\u9700\u8981\u5728\u591a\u4e2a\u65b9\u6cd5\u4e2d\u5171\u4eab\uff0c\u4f46\u4e0d\u5e0c\u671b\u88ab\u591a\u7ebf\u7a0b\u5171\u4eab<\/em><\/strong><\/li>\n<\/ul>\n<p><strong>1.\u5b58\u50a8\u7528\u6237session<\/strong><\/p>\n<pre class=\"code\">\u00a0private static final ThreadLocal threadSession = new ThreadLocal();\n \n    public static Session getSession() throws InfrastructureException {\n        Session s = (Session) threadSession.get();\n        try {\n            if (s == null) {\n                s = getSessionFactory().openSession();\n                threadSession.set(s);\n            }\n        } catch (HibernateException ex) {\n            throw new InfrastructureException(ex);\n        }\n        return s;\n    }<\/pre>\n<p><strong>2.\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5904\u7406\u6570\u636e\u5e93\u4e8b\u52a1<\/strong><\/p>\n<p><strong>3.\u6570\u636e\u8de8\u5c42\u4f20\u9012<\/strong><\/p>\n<p>\u6bcf\u4e2a\u7ebf\u7a0b\u5185\u9700\u8981\u4fdd\u5b58\u7c7b\u4f3c\u4e8e\u5168\u5c40\u53d8\u91cf\u7684\u4fe1\u606f\uff08\u4f8b\u5982\u5728\u62e6\u622a\u5668\u4e2d\u83b7\u53d6\u7684\u7528\u6237\u4fe1\u606f\uff09\uff0c\u53ef\u4ee5\u8ba9\u4e0d\u540c\u65b9\u6cd5\u76f4\u63a5\u4f7f\u7528\uff0c\u907f\u514d\u53c2\u6570\u4f20\u9012\u7684\u9ebb\u70e6\u5374\u4e0d\u60f3\u88ab\u591a\u7ebf\u7a0b\u5171\u4eab\uff08\u56e0\u4e3a\u4e0d\u540c\u7ebf\u7a0b\u83b7\u53d6\u5230\u7684\u7528\u6237\u4fe1\u606f\u4e0d\u4e00\u6837\uff09\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u7528 ThreadLocal \u4fdd\u5b58\u4e00\u4e9b\u4e1a\u52a1\u5185\u5bb9\uff08\u7528\u6237\u6743\u9650\u4fe1\u606f\u3001\u4ece\u7528\u6237\u7cfb\u7edf\u83b7\u53d6\u5230\u7684\u7528\u6237\u540d\u3001\u7528\u6237ID \u7b49\uff09\uff0c\u8fd9\u4e9b\u4fe1\u606f\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u5185\u76f8\u540c\uff0c\u4f46\u662f\u4e0d\u540c\u7684\u7ebf\u7a0b\u4f7f\u7528\u7684\u4e1a\u52a1\u5185\u5bb9\u662f\u4e0d\u76f8\u540c\u7684\u3002<\/p>\n<p>\u5728\u7ebf\u7a0b\u751f\u547d\u5468\u671f\u5185\uff0c\u90fd\u901a\u8fc7\u8fd9\u4e2a\u9759\u6001 ThreadLocal \u5b9e\u4f8b\u7684 get() \u65b9\u6cd5\u53d6\u5f97\u81ea\u5df1 set \u8fc7\u7684\u90a3\u4e2a\u5bf9\u8c61\uff0c\u907f\u514d\u4e86\u5c06\u8fd9\u4e2a\u5bf9\u8c61\uff08\u5982 user \u5bf9\u8c61\uff09\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7684\u9ebb\u70e6\u3002<\/p>\n<p>\u6bd4\u5982\u8bf4\u6211\u4eec\u662f\u4e00\u4e2a\u7528\u6237\u7cfb\u7edf\uff0c\u90a3\u4e48\u5f53\u4e00\u4e2a\u8bf7\u6c42\u8fdb\u6765\u7684\u65f6\u5019\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u4f1a\u8d1f\u8d23\u6267\u884c\u8fd9\u4e2a\u8bf7\u6c42\uff0c\u7136\u540e\u8fd9\u4e2a\u8bf7\u6c42\u5c31\u4f1a\u4f9d\u6b21\u8c03\u7528service-1()\u3001service-2()\u3001service-3()\u3001service-4()\uff0c\u8fd94\u4e2a\u65b9\u6cd5\u53ef\u80fd\u662f\u5206\u5e03\u5728\u4e0d\u540c\u7684\u7c7b\u4e2d\u7684\u3002\u8fd9\u4e2a\u4f8b\u5b50\u548c\u5b58\u50a8session\u6709\u4e9b\u50cf\u3002<\/p>\n<pre class=\"code\">\u00a0package com.kong.threadlocal;\n \n \npublic class ThreadLocalDemo05 {\n    public static void main(String[] args) {\n        User user = new User(\"jack\");\n        new Service1().service1(user);\n    }\n \n}\n \nclass Service1 {\n    public void service1(User user){\n        \/\/\u7ed9ThreadLocal\u8d4b\u503c\uff0c\u540e\u7eed\u7684\u670d\u52a1\u76f4\u63a5\u901a\u8fc7ThreadLocal\u83b7\u53d6\u5c31\u884c\u4e86\u3002\n        UserContextHolder.holder.set(user);\n        new Service2().service2();\n    }\n}\n \nclass Service2 {\n    public void service2(){\n        User user = UserContextHolder.holder.get();\n        System.out.println(\"service2\u62ff\u5230\u7684\u7528\u6237:\"+user.name);\n        new Service3().service3();\n    }\n}\n \nclass Service3 {\n    public void service3(){\n        User user = UserContextHolder.holder.get();\n        System.out.println(\"service3\u62ff\u5230\u7684\u7528\u6237:\"+user.name);\n        \/\/\u5728\u6574\u4e2a\u6d41\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\uff0c\u4e00\u5b9a\u8981\u6267\u884cremove\n        UserContextHolder.holder.remove();\n    }\n}\n \nclass UserContextHolder {\n    \/\/\u521b\u5efaThreadLocal\u4fdd\u5b58User\u5bf9\u8c61\n    public static ThreadLocal holder = new ThreadLocal&lt;&gt;();\n}\n \nclass User {\n    String name;\n    public User(String name){\n        this.name = name;\n    }\n}\n \n\u6267\u884c\u7684\u7ed3\u679c\uff1a\n \nservice2\u62ff\u5230\u7684\u7528\u6237:jack\nservice3\u62ff\u5230\u7684\u7528\u6237:jack<\/pre>\n<p><strong>4.spring\u4f7f\u7528ThreadLocal\u89e3\u51b3\u7ebf\u7a0b\u5b89\u5168<\/strong><\/p>\n<p>\u975e\u7ebf\u7a0b\u5b89\u5168<\/p>\n<pre class=\"code\"> \npublic class TopicDao {\n   \/\/\u2460\u4e00\u4e2a\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u53d8\u91cf\n   private Connection conn; \n   public void addTopic(){\n        \/\/\u2461\u5f15\u7528\u975e\u7ebf\u7a0b\u5b89\u5168\u53d8\u91cf\n\t   Statement stat = conn.createStatement();\n\t   \u2026\n   }\u00a0<\/pre>\n<p>\u7ebf\u7a0b\u5b89\u5168<\/p>\n<pre class=\"code\"> \nimport java.sql.Connection;\nimport java.sql.Statement;\npublic class TopicDao {\n \n  \/\/\u2460\u4f7f\u7528ThreadLocal\u4fdd\u5b58Connection\u53d8\u91cf\nprivate static ThreadLocal connThreadLocal = new ThreadLocal();\npublic static Connection getConnection(){\n         \n\t    \/\/\u2461\u5982\u679cconnThreadLocal\u6ca1\u6709\u672c\u7ebf\u7a0b\u5bf9\u5e94\u7684Connection\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Connection\uff0c\n        \/\/\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u4e2d\u3002\nif (connThreadLocal.get() == null) {\n\t\t\tConnection conn = ConnectionManager.getConnection();\n\t\t\tconnThreadLocal.set(conn);\n              return conn;\n\t\t}else{\n              \/\/\u2462\u76f4\u63a5\u8fd4\u56de\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\n\t\t\treturn connThreadLocal.get();\n\t\t}\n\t}\n\tpublic void addTopic() {\n \n\t\t\/\/\u2463\u4eceThreadLocal\u4e2d\u83b7\u53d6\u7ebf\u7a0b\u5bf9\u5e94\u7684\n         Statement stat = getConnection().createStatement();\n\t}\u00a0<\/pre>\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u672c\u8eab\u5f88\u7c97\u7cd9\uff0c\u5c06Connection\u7684ThreadLocal\u76f4\u63a5\u653e\u5728Dao\u53ea\u80fd\u505a\u5230\u672cDao\u7684\u591a\u4e2a\u65b9\u6cd5\u5171\u4eabConnection\u65f6\u4e0d\u53d1\u751f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u4f46\u65e0\u6cd5\u548c\u5176\u4ed6Dao\u5171\u7528\u540c\u4e00\u4e2aConnection\uff0c\u8981\u505a\u5230\u540c\u4e00\u4e8b\u52a1\u591aDao\u5171\u4eab\u540c\u4e00\u4e2aConnection\uff0c\u5fc5\u987b\u5728\u4e00\u4e2a\u5171\u540c\u7684\u5916\u90e8\u7c7b\u4f7f\u7528ThreadLocal\u4fdd\u5b58Connection\u3002<\/p>\n<hr \/>\n<h2>ThreadLocal\u4f7f\u7528\u539f\u7406<\/h2>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/img.picgo.net\/2024\/10\/22\/imagec8820bb4e7862ec1.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/img.picgo.net\/2024\/10\/22\/imagec8820bb4e7862ec1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image\" border=\"0\" \/><\/div><\/p>\n<h2>ThreadLocal\u5185\u5b58\u6cc4\u6f0f\u539f\u56e0<\/h2>\n<ol>\n<li>\u6ca1\u6709\u624b\u52a8remove<\/li>\n<li>currentThread\u4f9d\u7136\u8fd0\u884c<\/li>\n<\/ol>\n<p>\u00a0Entry\u5c06ThreadLocal\u4f5c\u4e3aKey\uff0c\u503c\u4f5c\u4e3avalue\u4fdd\u5b58\uff0c\u5b83\u7ee7\u627f\u81eaWeakReference\uff0c\u6ce8\u610f\u6784\u9020\u51fd\u6570\u91cc\u7684\u7b2c\u4e00\u884c\u4ee3\u7801super(k)\uff0c\u8fd9\u610f\u5473\u7740ThreadLocal\u5bf9\u8c61\u662f\u4e00\u4e2a\u300c\u5f31\u5f15\u7528\u300d\u3002<\/p>\n<pre class=\"code\">\u00a0static class Entry extends WeakReference&lt;ThreadLocal&lt;?&gt;&gt; {\n    \/** The value associated with this ThreadLocal. *\/\n    Object value;\n    Entry(ThreadLocal&lt;?&gt; k, Object v) {\n        super(k);\n        value = v;\n    }\n}<\/pre>\n<p>\u7b2c\u4e00\u70b9\u5f88\u597d\u7406\u89e3\uff0c\u53ea\u8981\u5728\u4f7f\u7528\u5b8c\u4e0b ThreadLocal \uff0c\u8c03\u7528\u5176 remove \u65b9\u6cd5\u5220\u9664\u5bf9\u5e94\u7684 Entry \uff0c\u5c31\u80fd\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<br \/>\u7b2c\u4e8c\u70b9\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\uff0c\u7531\u4e8eThreadLocalMap \u662f Thread \u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u88ab\u5f53\u524d\u7ebf\u7a0b\u6240\u5f15\u7528\uff0c\u6240\u4ee5ThreadLocalMap\u7684\u751f\u547d\u5468\u671f\u8ddf Thread \u4e00\u6837\u957f\u3002\u5982\u679cthreadlocal\u53d8\u91cf\u88ab\u56de\u6536\uff0c\u90a3\u4e48\u5f53\u524d\u7ebf\u7a0b\u7684threadlocal \u53d8\u91cf\u526f\u672c\u6307\u5411\u7684\u5c31\u662fkey=null, \u4e5f\u5373entry(null,value),\u90a3\u8fd9\u4e2aentry\u5bf9\u5e94\u7684value\u6c38\u8fdc\u65e0\u6cd5\u8bbf\u95ee\u5230\u3002\u5b9e\u9645\u79c1\u7528ThreadLocal\u573a\u666f\u90fd\u662f\u91c7\u7528\u7ebf\u7a0b\u6c60\uff0c\u800c\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u90fd\u662f\u590d\u7528\u7684\uff0c\u8fd9\u6837\u5c31\u53ef\u80fd\u5bfc\u81f4\u975e\u5e38\u591a\u7684entry(null,value)\u51fa\u73b0\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u6cc4\u9732\u3002<br \/>\u7efc\u4e0a\uff0c ThreadLocal \u5185\u5b58\u6cc4\u6f0f\u7684\u6839\u6e90\u662f\uff1a<br \/>\u7531\u4e8eThreadLocalMap \u7684\u751f\u547d\u5468\u671f\u8ddf Thread \u4e00\u6837\u957f\uff0c\u5bf9\u4e8e\u91cd\u590d\u5229\u7528\u7684\u7ebf\u7a0b\u6765\u8bf4\uff0c\u5982\u679c\u6ca1\u6709\u624b\u52a8\u5220\u9664\uff08remove()\u65b9\u6cd5\uff09\u5bf9\u5e94 key \u5c31\u4f1a\u5bfc\u81f4entry(null\uff0cvalue)\u7684\u5bf9\u8c61\u8d8a\u6765\u8d8a\u591a\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0e<\/p>\n<h2>\u4e3a\u4ec0\u4e48\u4e0d\u8bb2key\u8bbe\u7f6e\u5f3a\u5f15\u7528<\/h2>\n<p>1 .key \u5982\u679c\u662f\u5f3a\u5f15\u7528<br \/>\u90a3\u4e48\u4e3a\u4ec0\u4e48ThreadLocalMap\u7684key\u8981\u8bbe\u8ba1\u6210\u5f31\u5f15\u7528\u5462\uff1f\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u5982\u679ckey\u8bbe\u8ba1\u6210\u5f3a\u5f15\u7528\u4e14\u6ca1\u6709\u624b\u52a8remove()\uff0c\u90a3\u4e48key\u4f1a\u548cvalue\u4e00\u6837\u4f34\u968f\u7ebf\u7a0b\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>\u5047\u8bbe\u5728\u4e1a\u52a1\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b8cThreadLocal, ThreadLocal ref\u88ab\u56de\u6536\u4e86\uff0c\u4f46\u662f\u56e0\u4e3athreadLocalMap\u7684Entry\u5f3a\u5f15\u7528\u4e86threadLocal(key\u5c31\u662fthreadLocal), \u9020\u6210ThreadLocal\u65e0\u6cd5\u88ab\u56de\u6536\u3002\u5728\u6ca1\u6709\u624b\u52a8\u5220\u9664Entry\u4ee5\u53caCurrentThread(\u5f53\u524d\u7ebf\u7a0b)\u4f9d\u7136\u8fd0\u884c\u7684\u524d\u63d0\u4e0b, \u59cb\u7ec8\u6709\u5f3a\u5f15\u7528\u94feCurrentThread Ref \u2192 CurrentThread \u2192Map(ThreadLocalMap)-&gt; entry, Entry\u5c31\u4e0d\u4f1a\u88ab\u56de\u6536( Entry\u4e2d\u5305\u62ec\u4e86ThreadLocal\u5b9e\u4f8b\u548cvalue), \u5bfc\u81f4Entry\u5185\u5b58\u6cc4\u6f0f\u4e5f\u5c31\u662f\u8bf4: ThreadLocalMap\u4e2d\u7684key\u4f7f\u7528\u4e86\u5f3a\u5f15\u7528, \u662f\u65e0\u6cd5\u5b8c\u5168\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u7684\u3002\u8bf7\u7ed3\u5408\u56fe1\u770b\u3002<\/p>\n<p>2. \u90a3\u4e48\u4e3a\u4ec0\u4e48 key \u8981\u7528\u5f31\u5f15\u7528<br \/>\u4e8b\u5b9e\u4e0a\uff0c\u5728 ThreadLocalMap \u4e2d\u7684set\/getEntry \u65b9\u6cd5\u4e2d\uff0c\u4f1a\u5bf9 key \u4e3a null\uff08\u4e5f\u5373\u662f ThreadLocal \u4e3a null \uff09\u8fdb\u884c\u5224\u65ad\uff0c\u5982\u679c\u4e3a null \u7684\u8bdd\uff0c\u90a3\u4e48\u4f1a\u628a value \u7f6e\u4e3a null \u7684\uff0e\u8fd9\u5c31\u610f\u5473\u7740\u4f7f\u7528threadLocal , CurrentThread \u4f9d\u7136\u8fd0\u884c\u7684\u524d\u63d0\u4e0b\uff0e\u5c31\u7b97\u5fd8\u8bb0\u8c03\u7528 remove \u65b9\u6cd5\uff0c\u5f31\u5f15\u7528\u6bd4\u5f3a\u5f15\u7528\u53ef\u4ee5\u591a\u4e00\u5c42\u4fdd\u969c\uff1a\u5f31\u5f15\u7528\u7684 ThreadLocal \u4f1a\u88ab\u56de\u6536\uff0e\u5bf9\u5e94value\u5728\u4e0b\u4e00\u6b21 ThreadLocaI \u8c03\u7528 get()\/set()\/remove() \u4e2d\u7684\u4efb\u4e00\u65b9\u6cd5\u7684\u65f6\u5019\u4f1a\u88ab\u6e05\u9664\uff0c\u4ece\u800c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff0e<\/p>\n<p>3. \u5982\u4f55\u6b63\u786e\u7684\u4f7f\u7528ThreadLocal<\/p>\n<ul>\n<li>\u5c06ThreadLocal\u53d8\u91cf\u5b9a\u4e49\u6210private static\u7684\uff0c\u8fd9\u6837\u7684\u8bddThreadLocal\u7684\u751f\u547d\u5468\u671f\u5c31\u66f4\u957f\uff0c\u7531\u4e8e\u4e00\u76f4\u5b58\u5728ThreadLocal\u7684\u5f3a\u5f15\u7528\uff0c\u6240\u4ee5ThreadLocal\u4e5f\u5c31\u4e0d\u4f1a\u88ab\u56de\u6536\uff0c\u4e5f\u5c31\u80fd\u4fdd\u8bc1\u4efb\u4f55\u65f6\u5019\u90fd\u80fd\u6839\u636eThreadLocal\u7684\u5f31\u5f15\u7528\u8bbf\u95ee\u5230Entry\u7684value\u503c\uff0c\u7136\u540eremove\u5b83\uff0c\u9632\u6b62\u5185\u5b58\u6cc4\u9732<\/li>\n<li>\u6bcf\u6b21\u4f7f\u7528\u5b8cThreadLocal\uff0c\u90fd\u8c03\u7528\u5b83\u7684remove()\u65b9\u6cd5\uff0c\u6e05\u9664\u6570\u636e\u3002<\/li>\n<\/ul>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb ThreadLocal\u53eb\u505a\u7ebf\u7a0b\u53d8\u91cf\uff0c\u610f\u601d\u662fThreadLocal\u4e2d\u586b\u5145\u7684\u53d8\u91cf\u5c5e\u4e8e\u5f53\u524d\u7ebf\u7a0b\uff0c\u8be5\u53d8\u91cf\u5bf9\u5176\u4ed6\u7ebf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":407,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[14],"class_list":["post-406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-threadlocal"],"_links":{"self":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":2,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"predecessor-version":[{"id":408,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/posts\/406\/revisions\/408"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/media\/407"}],"wp:attachment":[{"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zackzzp.top\/index.php\/wp-json\/wp\/v2\/tags?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}