{"id":3275,"date":"2024-06-20T15:00:59","date_gmt":"2024-06-20T07:00:59","guid":{"rendered":"https:\/\/www.talktop.cn\/?p=3275"},"modified":"2024-12-07T17:19:48","modified_gmt":"2024-12-07T09:19:48","slug":"java%e4%b8%ad%e7%9a%84threadlocal%e6%8a%80%e6%9c%af%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.talktop.cn\/?p=3275","title":{"rendered":"Java\u4e2d\u7684ThreadLocal\u6280\u672f\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c<code>ThreadLocal<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002\u5b83\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u63d0\u4f9b\u4e86\u72ec\u7acb\u7684\u53d8\u91cf\u526f\u672c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u53d8\u91cf\u65f6\u7684\u7ade\u4e89\u95ee\u9898\u3002<code>ThreadLocal<\/code>\u4e3b\u8981\u7528\u4e8e\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u53d8\u91cf\u9694\u79bb\u95ee\u9898\uff0c\u4f7f\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u4e00\u4efd\u53d8\u91cf\uff0c\u4ece\u800c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u4e00\u3001ThreadLocal\u6784\u6210<\/h5>\n\n\n\n<p><code>ThreadLocal<\/code>\u7c7b\u4f4d\u4e8e<code>java.lang<\/code>\u5305\u4e2d\uff0c\u5176\u6838\u5fc3\u7ed3\u6784\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u90e8\u5206\uff1a<\/p>\n\n\n\n<p>\uff081\uff09<strong>ThreadLocal\u7c7b<\/strong>\uff1a<code>ThreadLocal<\/code>\u7c7b\u672c\u8eab\uff0c\u63d0\u4f9b\u4e86<code>get<\/code>\u548c<code>set<\/code>\u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u548c\u8bbe\u7f6e\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\uff082\uff09<strong>ThreadLocalMap<\/strong>\uff1a<code>ThreadLocal<\/code>\u7684\u5185\u90e8\u7c7b\uff0c\u7528\u4e8e\u5b58\u50a8\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u53d8\u91cf\u526f\u672c\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u5185\u90e8\u7ef4\u62a4\u4e00\u4e2a<code>ThreadLocalMap<\/code>\uff0c\u8be5<code>ThreadLocalMap<\/code>\u4ee5<code>ThreadLocal<\/code>\u5bf9\u8c61\u4e3a\u952e\uff0c\u5b58\u50a8\u5bf9\u5e94\u7684\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\u503c\u3002<\/p>\n\n\n\n<p>\uff083\uff09<strong>Thread\u7c7b<\/strong>\uff1aJava\u7ebf\u7a0b\u7c7b\u4e2d\u6709\u4e00\u4e2a\u7c7b\u578b\u4e3a<code>ThreadLocalMap<\/code>\u7684\u6210\u5458\u53d8\u91cf\uff0c\u7528\u4e8e\u5b58\u50a8\u8be5\u7ebf\u7a0b\u6240\u6709\u7684<code>ThreadLocal<\/code>\u53d8\u91cf\u3002<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">ThreadLocal\u7c7b\u7684\u7ee7\u627f\u5b9e\u73b0\u5173\u7cfb<\/h6>\n\n\n\n<p><code>ThreadLocal<\/code>\u7c7b\u6ca1\u6709\u7ee7\u627f\u5176\u4ed6\u7c7b\uff0c\u4f46\u5176\u8bbe\u8ba1\u5229\u7528\u4e86\u6cdb\u578b\u6765\u5b9a\u4e49\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\u7684\u7c7b\u578b\u3002\u4ee5\u4e0b\u662f<code>ThreadLocal<\/code>\u7c7b\u7684\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-cef070eb68d4d6fb8c1ea1b550108c6d\"><code>public class ThreadLocal&lt;T&gt; {\n    \/\/ \u5185\u90e8\u9759\u6001\u7c7b ThreadLocalMap\n    static class ThreadLocalMap {\n        \/\/ Entry\u7ee7\u627fWeakReference\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\n        static class Entry extends WeakReference&lt;ThreadLocal&lt;?&gt;&gt; {\n            Object value;\n            Entry(ThreadLocal&lt;?&gt; k, Object v) {\n                super(k);\n                value = v;\n            }\n        }\n        \/\/ \u5176\u4ed6\u76f8\u5173\u4ee3\u7801\n    }\n\n    \/\/ \u6838\u5fc3\u65b9\u6cd5\n    public T get() {\n        \/\/ \u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\n        Thread t = Thread.currentThread();\n        \/\/ \u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684ThreadLocalMap\n        ThreadLocalMap map = getMap(t);\n        if (map != null) {\n            ThreadLocalMap.Entry e = map.getEntry(this);\n            if (e != null) {\n                @SuppressWarnings(\"unchecked\")\n                T result = (T)e.value;\n                return result;\n            }\n        }\n        return setInitialValue();\n    }\n\n    public void set(T value) {\n        Thread t = Thread.currentThread();\n        ThreadLocalMap map = getMap(t);\n        if (map != null) {\n            map.set(this, value);\n        } else {\n            createMap(t, value);\n        }\n    }\n\n    protected T initialValue() {\n        return null;\n    }\n\n    private T setInitialValue() {\n        T value = initialValue();\n        Thread t = Thread.currentThread();\n        ThreadLocalMap map = getMap(t);\n        if (map != null) {\n            map.set(this, value);\n        } else {\n            createMap(t, value);\n        }\n        return value;\n    }\n\n    ThreadLocalMap getMap(Thread t) {\n        return t.threadLocals;\n    }\n\n    void createMap(Thread t, T firstValue) {\n        t.threadLocals = new ThreadLocalMap(this, firstValue);\n    }\n}<\/code><\/pre>\n\n\n\n<h6 class=\"wp-block-heading\">ThreadLocalMap\u7684\u6784\u6210<\/h6>\n\n\n\n<p><code>ThreadLocalMap<\/code>\u662f<code>ThreadLocal<\/code>\u7684\u4e00\u4e2a\u9759\u6001\u5185\u90e8\u7c7b\uff0c\u4e13\u95e8\u7528\u6765\u5b58\u50a8\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u5c40\u90e8\u53d8\u91cf\u3002\u5b83\u7684\u5b9e\u73b0\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u54c8\u5e0c\u8868\uff0c\u4f46\u505a\u4e86\u7279\u6b8a\u4f18\u5316\u3002\u5176\u5173\u952e\u90e8\u5206\u5305\u62ec\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Entry\u7c7b<\/strong>\uff1a\u7ee7\u627f\u81ea<code>WeakReference<\/code>\uff0c\u7528\u4e8e\u5b58\u50a8<code>ThreadLocal<\/code>\u5bf9\u8c61\u548c\u5bf9\u5e94\u7684\u503c\u3002\u4f7f\u7528\u5f31\u5f15\u7528\u53ef\u4ee5\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\uff0c\u5373\u5f53<code>ThreadLocal<\/code>\u5bf9\u8c61\u6ca1\u6709\u5176\u4ed6\u5f3a\u5f15\u7528\u65f6\uff0c\u53ef\u4ee5\u88ab\u5783\u573e\u56de\u6536\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-44c970610309a309df7cc9981a78cda8\"><code><span class=\"hljs-keyword\">static<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Entry<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title class_\">WeakReference<\/span>&lt;ThreadLocal&lt;?&gt;&gt; {\n    Object value;\n\n    Entry(ThreadLocal&lt;?&gt; k, Object v) {\n        <span class=\"hljs-built_in\">super<\/span>(k);\n        value = v;\n    }\n}<\/code><\/pre>\n\n\n\n<p>    2.<strong>\u5b58\u50a8\u7ed3\u6784<\/strong>\uff1a<code>ThreadLocalMap<\/code>\u4f7f\u7528\u4e00\u4e2a<code>Entry<\/code>\u6570\u7ec4\u6765\u5b58\u50a8\u6570\u636e\uff0c\u6bcf\u4e2a<code>ThreadLocal<\/code>\u5b9e\u4f8b\u5bf9\u5e94\u4e00\u4e2a<code>Entry<\/code>\u3002\u5f53<code>ThreadLocal<\/code>\u5b9e\u4f8b\u8c03\u7528<code>set<\/code>\u65b9\u6cd5\u65f6\uff0c\u4f1a\u5c06\u6570\u636e\u5b58\u50a8\u5728\u5f53\u524d\u7ebf\u7a0b\u7684<code>ThreadLocalMap<\/code>\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-e1633c7a34e97f68228097f7379d1f80\"><code>ThreadLocalMap(ThreadLocal&lt;?&gt; firstKey, Object firstValue) {\n    table = new Entry&#91;INITIAL_CAPACITY];\n    int i = firstKey.threadLocalHashCode &amp; (INITIAL_CAPACITY - 1);\n    table&#91;i] = new Entry(firstKey, firstValue);\n    size = 1;\n    setThreshold(INITIAL_CAPACITY);\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u4e8c\u3001\u4f5c\u7528\u53ca\u5e94\u7528\u573a\u666f<\/h5>\n\n\n\n<p><code>ThreadLocal<\/code>\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u63d0\u4f9b\u7ebf\u7a0b\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u8fd9\u4e9b\u53d8\u91cf\u5728\u7ebf\u7a0b\u5185\u662f\u72ec\u7acb\u7684\uff0c\u7ebf\u7a0b\u4e4b\u95f4\u4e0d\u4f1a\u4e92\u76f8\u5f71\u54cd\u3002\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u5305\u62ec\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5e93\u8fde\u63a5\u7ba1\u7406<\/strong>\uff1a\u5728\u4e00\u4e2a\u591a\u7ebf\u7a0b\u5e94\u7528\u4e2d\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u9700\u8981\u72ec\u7acb\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u901a\u8fc7<code>ThreadLocal<\/code>\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u4f1a\u8bdd\u7ba1\u7406<\/strong>\uff1a\u5728Web\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ThreadLocal<\/code>\u5b58\u50a8\u6bcf\u4e2a\u7528\u6237\u7684\u4f1a\u8bdd\u4fe1\u606f\uff0c\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u52a1\u7ba1\u7406<\/strong>\uff1a\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ThreadLocal<\/code>\u6765\u7ba1\u7406\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u4e8b\u52a1\u72b6\u6001\uff0c\u4fdd\u8bc1\u4e8b\u52a1\u9694\u79bb\u6027\u3002<\/li>\n<\/ol>\n\n\n\n<h6 class=\"wp-block-heading\">\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528\u5b9e\u4f8b<\/h6>\n\n\n\n<h6 class=\"wp-block-heading\">1. \u6570\u636e\u5e93\u8fde\u63a5\u7ba1\u7406<\/h6>\n\n\n\n<p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u9700\u8981\u72ec\u7acb\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u901a\u8fc7<code>ThreadLocal<\/code>\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u6570\u636e\u5e93\u8fde\u63a5\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-9151b7d99730bdd5727cce328e2c9cdc\"><code>import java.sql.Connection;\nimport java.sql.DriverManager;\nimport java.sql.SQLException;\n\npublic class ConnectionManager {\n    private static final String URL = \"jdbc:mysql:\/\/localhost:3306\/testdb\";\n    private static final String USERNAME = \"root\";\n    private static final String PASSWORD = \"password\";\n\n    private static ThreadLocal&lt;Connection&gt; connectionHolder = ThreadLocal.withInitial(() -&gt; {\n        try {\n            return DriverManager.getConnection(URL, USERNAME, PASSWORD);\n        } catch (SQLException e) {\n            throw new RuntimeException(\"Failed to create a database connection\", e);\n        }\n    });\n\n    public static Connection getConnection() {\n        return connectionHolder.get();\n    }\n\n    public static void closeConnection() {\n        Connection connection = connectionHolder.get();\n        if (connection != null) {\n            try {\n                connection.close();\n            } catch (SQLException e) {\n                e.printStackTrace();\n            } finally {\n                connectionHolder.remove();\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h6 class=\"wp-block-heading\">2. \u7528\u6237\u4f1a\u8bdd\u7ba1\u7406<\/h6>\n\n\n\n<p>\u5728Web\u5e94\u7528\u4e2d\uff0c\u6bcf\u4e2a\u8bf7\u6c42\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ThreadLocal<\/code>\u5b58\u50a8\u6bcf\u4e2a\u7528\u6237\u7684\u4f1a\u8bdd\u4fe1\u606f\uff0c\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-ba1f38f8b6ed8437ea5f4c16b4f07d0b\"><code>public class UserContext {\n    private static ThreadLocal&lt;String&gt; currentUser = new ThreadLocal&lt;&gt;();\n\n    public static void setCurrentUser(String user) {\n        currentUser.set(user);\n    }\n\n    public static String getCurrentUser() {\n        return currentUser.get();\n    }\n\n    public static void clear() {\n        currentUser.remove();\n    }\n}\n\n\/\/ \u5728Servlet\u4e2d\u4f7f\u7528\npublic class UserServlet extends HttpServlet {\n    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n        try {\n            \/\/ \u8bbe\u7f6e\u5f53\u524d\u7528\u6237\n            UserContext.setCurrentUser(request.getParameter(\"user\"));\n            \/\/ \u5904\u7406\u7528\u6237\u8bf7\u6c42\n            processRequest(request, response);\n        } finally {\n            \/\/ \u6e05\u9664\u7528\u6237\u4fe1\u606f\n            UserContext.clear();\n        }\n    }\n    \n    private void processRequest(HttpServletRequest request, HttpServletResponse response) {\n        \/\/ \u83b7\u53d6\u5f53\u524d\u7528\u6237\n        String user = UserContext.getCurrentUser();\n        \/\/ \u5904\u7406\u4e1a\u52a1\u903b\u8f91\n    }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u4e09\u3001\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/h5>\n\n\n\n<p>\u867d\u7136<code>ThreadLocal<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u4f46\u5728\u4f7f\u7528\u65f6\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5185\u5b58\u6cc4\u6f0f<\/strong>\uff1a<code>ThreadLocal<\/code>\u53d8\u91cf\u5fc5\u987b\u624b\u52a8\u6e05\u7406\uff0c\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002\u4f7f\u7528\u5b8c\u6bd5\u540e\u5e94\u8c03\u7528<code>remove<\/code>\u65b9\u6cd5\u79fb\u9664\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u6b63\u786e\u521d\u59cb\u5316<\/strong>\uff1a\u786e\u4fdd<code>ThreadLocal<\/code>\u53d8\u91cf\u7684\u521d\u59cb\u503c\u6b63\u786e\uff0c\u5426\u5219\u53ef\u80fd\u5bfc\u81f4\u7ebf\u7a0b\u95f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd\u95ee\u9898<\/strong>\uff1a<code>ThreadLocal<\/code>\u867d\u7136\u80fd\u591f\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\uff0c\u4f46\u5176\u5e95\u5c42\u5b9e\u73b0\u4f9d\u8d56\u4e8e\u7ebf\u7a0bID\uff0c\u9891\u7e41\u7684\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u4f1a\u5f71\u54cd\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u9002\u7528\u4e8e\u7ebf\u7a0b\u751f\u547d\u5468\u671f\u8f83\u957f\u7684\u573a\u666f\u3002<\/li>\n\n\n\n<li><strong>\u907f\u514d\u590d\u6742\u903b\u8f91<\/strong>\uff1a<code>ThreadLocal<\/code>\u9002\u5408\u5b58\u50a8\u7b80\u5355\u7684\u5bf9\u8c61\u6216\u72b6\u6001\u4fe1\u606f\uff0c\u4e0d\u9002\u5408\u5b58\u50a8\u590d\u6742\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u907f\u514d\u4ee3\u7801\u96be\u4ee5\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0cThreadLocal\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002\u5b83\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u63d0\u4f9b\u4e86\u72ec\u7acb\u7684\u53d8\u91cf\u526f\u672c\uff0c\u4ece\u800c\u907f\u514d\u4e86 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[406,16],"class_list":["post-3275","post","type-post","status-publish","format-standard","hentry","category-59","tag-threadlocal","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java\u4e2d\u7684ThreadLocal\u6280\u672f\u8be6\u89e3<\/title>\n<meta name=\"description\" content=\"\u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0cThreadLocal\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002\u5b83\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u63d0\u4f9b\u4e86\u72ec\u7acb\u7684\u53d8\u91cf\u526f\u672c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u53d8\u91cf\u65f6\u7684\u7ade\u4e89\u95ee\u9898\u3002ThreadLocal\u4e3b\u8981\u7528\u4e8e\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u53d8\u91cf\u9694\u79bb\u95ee\u9898\uff0c\u4f7f\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u4e00\u4efd\u53d8\u91cf\uff0c\u4ece\u800c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002\" \/>\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.talktop.cn\/?p=3275\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.talktop.cn\/?p=3275#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.talktop.cn\/?p=3275\"},\"author\":{\"name\":\"AI\u98ce\u5411\u6807\",\"@id\":\"https:\/\/www.talktop.cn\/#\/schema\/person\/25908db5f654913a22bc38d48fad71ab\"},\"headline\":\"Java\u4e2d\u7684ThreadLocal\u6280\u672f\u8be6\u89e3\",\"datePublished\":\"2024-06-20T07:00:59+00:00\",\"dateModified\":\"2024-12-07T09:19:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.talktop.cn\/?p=3275\"},\"wordCount\":14,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.talktop.cn\/#\/schema\/person\/25908db5f654913a22bc38d48fad71ab\"},\"keywords\":[\"ThreadLocal\",\"\u7f16\u7a0b\"],\"articleSection\":[\"\u7f16\u7a0b\u6280\u672f\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.talktop.cn\/?p=3275#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.talktop.cn\/?p=3275\",\"url\":\"https:\/\/www.talktop.cn\/?p=3275\",\"name\":\"Java\u4e2d\u7684ThreadLocal\u6280\u672f\u8be6\u89e3\",\"isPartOf\":{\"@id\":\"https:\/\/www.talktop.cn\/#website\"},\"datePublished\":\"2024-06-20T07:00:59+00:00\",\"dateModified\":\"2024-12-07T09:19:48+00:00\",\"description\":\"\u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0cThreadLocal\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002\u5b83\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u63d0\u4f9b\u4e86\u72ec\u7acb\u7684\u53d8\u91cf\u526f\u672c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u53d8\u91cf\u65f6\u7684\u7ade\u4e89\u95ee\u9898\u3002ThreadLocal\u4e3b\u8981\u7528\u4e8e\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u53d8\u91cf\u9694\u79bb\u95ee\u9898\uff0c\u4f7f\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u4e00\u4efd\u53d8\u91cf\uff0c\u4ece\u800c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002\",\"breadcrumb\":{\"@id\":\"https:\/\/www.talktop.cn\/?p=3275#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.talktop.cn\/?p=3275\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.talktop.cn\/?p=3275#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.talktop.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\u4e2d\u7684ThreadLocal\u6280\u672f\u8be6\u89e3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.talktop.cn\/#website\",\"url\":\"https:\/\/www.talktop.cn\/\",\"name\":\"AI\u98ce\u5411\u6807\",\"description\":\"ChatGPT \u4eba\u5de5\u667a\u80fd \u7f16\u7a0b\u6280\u672f AIGC \u540e\u7aef\u6280\u672f \u524d\u7aef\u6280\u672f \u70ed\u95e8\u63d2\u4ef6 \u8fd0\u8425\u76f8\u5173 ChatGPT\u8ba2\u9605 Claude\u5145\u503c \u6d77\u5916\u5145\u503c \u8c37\u6b4c\u90ae\u7bb1\",\"publisher\":{\"@id\":\"https:\/\/www.talktop.cn\/#\/schema\/person\/25908db5f654913a22bc38d48fad71ab\"},\"alternateName\":\"AI\u98ce\u5411\u6807\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.talktop.cn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.talktop.cn\/#\/schema\/person\/25908db5f654913a22bc38d48fad71ab\",\"name\":\"AI\u98ce\u5411\u6807\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.talktop.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.talktop.cn\/wp-content\/uploads\/2023\/10\/cropped-f09060ba975518b80d3b0b63b47108a6.jpeg\",\"contentUrl\":\"https:\/\/www.talktop.cn\/wp-content\/uploads\/2023\/10\/cropped-f09060ba975518b80d3b0b63b47108a6.jpeg\",\"width\":512,\"height\":512,\"caption\":\"AI\u98ce\u5411\u6807\"},\"logo\":{\"@id\":\"https:\/\/www.talktop.cn\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/\/\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java\u4e2d\u7684ThreadLocal\u6280\u672f\u8be6\u89e3","description":"\u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0cThreadLocal\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002\u5b83\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u63d0\u4f9b\u4e86\u72ec\u7acb\u7684\u53d8\u91cf\u526f\u672c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u53d8\u91cf\u65f6\u7684\u7ade\u4e89\u95ee\u9898\u3002ThreadLocal\u4e3b\u8981\u7528\u4e8e\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u53d8\u91cf\u9694\u79bb\u95ee\u9898\uff0c\u4f7f\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u4e00\u4efd\u53d8\u91cf\uff0c\u4ece\u800c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002","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.talktop.cn\/?p=3275","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.talktop.cn\/?p=3275#article","isPartOf":{"@id":"https:\/\/www.talktop.cn\/?p=3275"},"author":{"name":"AI\u98ce\u5411\u6807","@id":"https:\/\/www.talktop.cn\/#\/schema\/person\/25908db5f654913a22bc38d48fad71ab"},"headline":"Java\u4e2d\u7684ThreadLocal\u6280\u672f\u8be6\u89e3","datePublished":"2024-06-20T07:00:59+00:00","dateModified":"2024-12-07T09:19:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.talktop.cn\/?p=3275"},"wordCount":14,"commentCount":0,"publisher":{"@id":"https:\/\/www.talktop.cn\/#\/schema\/person\/25908db5f654913a22bc38d48fad71ab"},"keywords":["ThreadLocal","\u7f16\u7a0b"],"articleSection":["\u7f16\u7a0b\u6280\u672f"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.talktop.cn\/?p=3275#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.talktop.cn\/?p=3275","url":"https:\/\/www.talktop.cn\/?p=3275","name":"Java\u4e2d\u7684ThreadLocal\u6280\u672f\u8be6\u89e3","isPartOf":{"@id":"https:\/\/www.talktop.cn\/#website"},"datePublished":"2024-06-20T07:00:59+00:00","dateModified":"2024-12-07T09:19:48+00:00","description":"\u5728Java\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0cThreadLocal\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002\u5b83\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u63d0\u4f9b\u4e86\u72ec\u7acb\u7684\u53d8\u91cf\u526f\u672c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u591a\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u540c\u4e00\u53d8\u91cf\u65f6\u7684\u7ade\u4e89\u95ee\u9898\u3002ThreadLocal\u4e3b\u8981\u7528\u4e8e\u89e3\u51b3\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u53d8\u91cf\u9694\u79bb\u95ee\u9898\uff0c\u4f7f\u6bcf\u4e2a\u7ebf\u7a0b\u62e5\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u4e00\u4efd\u53d8\u91cf\uff0c\u4ece\u800c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002","breadcrumb":{"@id":"https:\/\/www.talktop.cn\/?p=3275#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.talktop.cn\/?p=3275"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.talktop.cn\/?p=3275#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.talktop.cn\/"},{"@type":"ListItem","position":2,"name":"Java\u4e2d\u7684ThreadLocal\u6280\u672f\u8be6\u89e3"}]},{"@type":"WebSite","@id":"https:\/\/www.talktop.cn\/#website","url":"https:\/\/www.talktop.cn\/","name":"AI\u98ce\u5411\u6807","description":"ChatGPT \u4eba\u5de5\u667a\u80fd \u7f16\u7a0b\u6280\u672f AIGC \u540e\u7aef\u6280\u672f \u524d\u7aef\u6280\u672f \u70ed\u95e8\u63d2\u4ef6 \u8fd0\u8425\u76f8\u5173 ChatGPT\u8ba2\u9605 Claude\u5145\u503c \u6d77\u5916\u5145\u503c \u8c37\u6b4c\u90ae\u7bb1","publisher":{"@id":"https:\/\/www.talktop.cn\/#\/schema\/person\/25908db5f654913a22bc38d48fad71ab"},"alternateName":"AI\u98ce\u5411\u6807","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.talktop.cn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.talktop.cn\/#\/schema\/person\/25908db5f654913a22bc38d48fad71ab","name":"AI\u98ce\u5411\u6807","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.talktop.cn\/#\/schema\/person\/image\/","url":"https:\/\/www.talktop.cn\/wp-content\/uploads\/2023\/10\/cropped-f09060ba975518b80d3b0b63b47108a6.jpeg","contentUrl":"https:\/\/www.talktop.cn\/wp-content\/uploads\/2023\/10\/cropped-f09060ba975518b80d3b0b63b47108a6.jpeg","width":512,"height":512,"caption":"AI\u98ce\u5411\u6807"},"logo":{"@id":"https:\/\/www.talktop.cn\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/\/"]}]}},"_links":{"self":[{"href":"https:\/\/www.talktop.cn\/index.php?rest_route=\/wp\/v2\/posts\/3275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.talktop.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.talktop.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.talktop.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.talktop.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3275"}],"version-history":[{"count":0,"href":"https:\/\/www.talktop.cn\/index.php?rest_route=\/wp\/v2\/posts\/3275\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.talktop.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.talktop.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.talktop.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}