{"id":4051,"date":"2018-08-12T10:05:19","date_gmt":"2018-08-12T02:05:19","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4051"},"modified":"2018-08-12T10:05:19","modified_gmt":"2018-08-12T02:05:19","slug":"java%e4%b8%ad%e7%9a%84threadlocal%e5%ad%a6%e4%b9%a0%e7%90%86%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4051.html","title":{"rendered":"Java\u4e2d\u7684ThreadLocal\u5b66\u4e60\u7406\u89e3"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u524d\u6bb5\u65f6\u95f4\u5728\u7528Java\u8fdb\u884c\u52a0\u89e3\u5bc6\u7684\u65f6\u5019\u78b0\u5230\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff0c\u540e\u6765\u53d1\u73b0\u662f\u56e0\u4e3a\u5bf9Java\u7684Cipher\u5305\u4e0d\u719f\u6089\u5bfc\u81f4\uff0c\u6700\u540e\u901a\u8fc7ThreadLocal\u7684\u65b9\u5f0f\u5728\u6b63\u786e\u6027\u548c\u6027\u80fd\u4e0a\u53d6\u5f97\u4e86\u4e00\u4e2a\u5e73\u8861\u3002<\/p>\n<p>\u867d\u7136\u95ee\u9898\u662f\u89e3\u51b3\u4e86\uff0c\u4f46\u662fThreadLocal\u662f\u4ec0\u4e48\uff0c\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u80fd\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u6211\u8fd8\u4e0d\u662f\u7279\u522b\u6e05\u695a\uff0c\u6240\u4ee5\u5c31\u60f3\u4e13\u95e8\u5b66\u4e60\u4e00\u4e0bThreadLocal\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u56e0\u4e3a\u6574\u4f53\u4e0d\u7b97\u590d\u6742\uff0c\u6240\u4ee5\u5728\u6536\u96c6\u3001\u6574\u7406\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5c31\u5b8c\u6210\u4e86\u76f8\u5173\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u4ecb\u7ecd\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6><strong># ThreadLocal \u662f\u4ec0\u4e48\uff1f<\/strong><\/h6>\n<p><span style=\"color: #ff0000;\"><strong>ThreadLocal\u662f\u4e00\u4e2a\u5173\u4e8e\u521b\u5efa\u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf\u7684\u7c7b\u3002<\/strong>ThreadLocal\uff0c\u5f88\u591a\u5730\u65b9\u53eb\u505a\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\uff0c\u4e5f\u6709\u4e9b\u5730\u65b9\u53eb\u505a\u7ebf\u7a0b\u672c\u5730\u5b58\u50a8\uff0c\u5176\u5b9e\u610f\u601d\u5dee\u4e0d\u591a\u3002<\/span>\u53ef\u80fd\u5f88\u591a\u670b\u53cb\u90fd\u77e5\u9053ThreadLocal\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><span style=\"color: #ff0000;\">\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u521b\u5efa\u7684\u53d8\u91cf\u662f\u53ef\u4ee5\u88ab\u4efb\u4f55\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u5e76\u4fee\u6539\u7684\u3002\u800c\u4f7f\u7528ThreadLocal\u521b\u5efa\u7684\u53d8\u91cf\u53ea\u80fd\u88ab\u5f53\u524d\u7ebf\u7a0b\u8bbf\u95ee\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5219\u65e0\u6cd5\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/span><\/p>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1 threadLocal \u786e\u5b9e\u662f\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u521b\u5efa\u4e86\u4e00\u4e2a\u5404\u81ea\u7684\u526f\u672c\uff1f<\/strong><\/p>\n<pre class=\"lang:default decode:true \">public class ThreadLocalTest {\r\n    ThreadLocal&lt;Long&gt; longLocal = new ThreadLocal&lt;Long&gt;();\r\n    ThreadLocal&lt;String&gt; stringLocal = new ThreadLocal&lt;String&gt;();\r\n\r\n    public void set() {\r\n        longLocal.set(Thread.currentThread().getId());\r\n        stringLocal.set(Thread.currentThread().getName());\r\n    }\r\n\r\n    public long getLong() {\r\n        return longLocal.get();\r\n    }\r\n\r\n    public String getString() {\r\n        return stringLocal.get();\r\n    }\r\n\r\n    public static void main(String[] args) throws InterruptedException {\r\n        final ThreadLocalTest threadLocalTest = new ThreadLocalTest();\r\n        \r\n        threadLocalTest.set();\r\n        System.out.println(threadLocalTest.getLong());\r\n        System.out.println(threadLocalTest.getString());\r\n        \r\n        Thread thread1 = new Thread(){\r\n            public void run() {\r\n                threadLocalTest.set();\r\n                System.out.println(threadLocalTest.getLong());\r\n                System.out.println(threadLocalTest.getString());\r\n            };\r\n        };\r\n        thread1.start();\r\n        thread1.join();\r\n         \r\n        System.out.println(threadLocalTest.getLong());\r\n        System.out.println(threadLocalTest.getString());\r\n    }\r\n}<\/pre>\n<h6><strong># threadLocal \u4f55\u65f6\u7528\uff1f\u600e\u4e48\u7528\uff1f<\/strong><\/h6>\n<ul>\n<li><span style=\"color: #ff0000;\">\u6700\u5e38\u89c1\u7684ThreadLocal\u4f7f\u7528\u573a\u666f\u662f\u7528\u6765\u89e3\u51b3\u6570\u636e\u5e93\u8fde\u63a5\u3001Session\u7ba1\u7406\u7b49\u95ee\u9898\u3002<\/span><\/li>\n<li>\u8fd8\u6709\u5c31\u662f\u5bc6\u7801\u7b97\u6cd5\u4e2d\u7684 init \u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u56e0\u4e3aJava\u4e2d\u7684Cipher\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u662f init \u53c8\u975e\u5e38\u6162\uff0c\u6240\u4ee5\u6bd4\u8f83\u9002\u5408\u3002<\/li>\n<\/ul>\n<h6><strong># threadLocal \u4f7f\u7528\u7684\u6ce8\u610f\u4e8b\u9879\uff1f<\/strong><\/h6>\n<p>1\u3001\u5148 set \u518d get \u5426\u5219\u53ef\u80fd\u4f1a\u51fa\u5f02\u5e38\uff0c\u6216\u8005\u901a\u8fc7\u91cd\u5199 initialValue \u65b9\u6cd5\u6765\u907f\u514d\u6b64\u95ee\u9898\uff1b<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/www.cnblogs.com\/dolphin0520\/p\/3920407.html\">Java\u5e76\u53d1\u7f16\u7a0b\uff1a\u6df1\u5165\u5256\u6790ThreadLocal<\/a><\/li>\n<li><a href=\"https:\/\/droidyue.com\/blog\/2016\/03\/13\/learning-threadlocal-in-java\/\">\u7406\u89e3Java\u4e2d\u7684ThreadLocal<\/a><\/li>\n<li><a href=\"https:\/\/www.cnblogs.com\/jasongj\/p\/8079718.html\">Java\u8fdb\u9636\uff08\u4e03\uff09\u6b63\u786e\u7406\u89e3Thread Local\u7684\u539f\u7406\u4e0e\u9002\u7528\u573a\u666f<\/a><br \/>\n<a href=\"http:\/\/www.jasongj.com\/java\/threadlocal\/\">http:\/\/www.jasongj.com\/java\/threadlocal\/<\/a><\/li>\n<li><a href=\"http:\/\/ifeve.com\/java-threadlocal%E7%9A%84%E4%BD%BF%E7%94%A8\/\">Java ThreadLocal\u7684\u4f7f\u7528<\/a><\/li>\n<li><a href=\"https:\/\/allenwu.itscoder.com\/threadlocal-source\">\u6df1\u5165\u7406\u89e3 Java \u4e4b ThreadLocal \u5de5\u4f5c\u539f\u7406<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u524d\u6bb5\u65f6\u95f4\u5728\u7528Java\u8fdb\u884c\u52a0\u89e3\u5bc6\u7684\u65f6\u5019\u78b0\u5230\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\uff0c\u540e\u6765\u53d1\u73b0\u662f\u56e0\u4e3a\u5bf9Java [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[61,1193],"class_list":["post-4051","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-java","tag-threadlocal"],"views":4400,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4051"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4051\/revisions"}],"predecessor-version":[{"id":4052,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4051\/revisions\/4052"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}