{"id":37486,"date":"2023-12-11T14:41:40","date_gmt":"2023-12-11T06:41:40","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=37486"},"modified":"2023-12-11T14:41:40","modified_gmt":"2023-12-11T06:41:40","slug":"%e8%af%b7%e4%bb%8b%e7%bb%8d%e4%b8%80%e4%b8%8bthreadlocal%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e8%af%b7%e4%bb%8b%e7%bb%8d%e4%b8%80%e4%b8%8bthreadlocal%ef%bc%9f\/","title":{"rendered":"\u8bf7\u4ecb\u7ecd\u4e00\u4e0bThreadLocal\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;<\/p>\n<p>&lt;h3&gt;ThreadLocal\u7b80\u4ecb&lt;\/h3&gt;<\/p>\n<p>\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&lt;strong&gt;\u5982\u679c\u60f3\u5b9e\u73b0\u6bcf\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u6709\u81ea\u5df1\u7684\u4e13\u5c5e\u672c\u5730\u53d8\u91cf\u8be5\u5982\u4f55\u89e3\u51b3\u5462\uff1f&lt;\/strong&gt; JDK\u4e2d\u63d0\u4f9b\u7684&lt;code&gt;ThreadLocal&lt;\/code&gt;\u7c7b\u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u6837\u7684\u95ee\u9898\u3002 &lt;strong&gt;&lt;code&gt;ThreadLocal&lt;\/code&gt;\u7c7b\u4e3b\u8981\u89e3\u51b3\u7684\u5c31\u662f\u8ba9\u6bcf\u4e2a\u7ebf\u7a0b\u7ed1\u5b9a\u81ea\u5df1\u7684\u503c\uff0c\u53ef\u4ee5\u5c06&lt;code&gt;ThreadLocal&lt;\/code&gt;\u7c7b\u5f62\u8c61\u7684\u6bd4\u55bb\u6210\u5b58\u653e\u6570\u636e\u7684\u76d2\u5b50\uff0c\u76d2\u5b50\u4e2d\u53ef\u4ee5\u5b58\u50a8\u6bcf\u4e2a\u7ebf\u7a0b\u7684\u79c1\u6709\u6570\u636e\u3002&lt;\/strong&gt;<\/p>\n<p>&lt;strong&gt;\u5982\u679c\u4f60\u521b\u5efa\u4e86\u4e00\u4e2a&lt;code&gt;ThreadLocal&lt;\/code&gt;\u53d8\u91cf\uff0c\u90a3\u4e48\u8bbf\u95ee\u8fd9\u4e2a\u53d8\u91cf\u7684\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u4f1a\u6709\u8fd9\u4e2a\u53d8\u91cf\u7684\u672c\u5730\u526f\u672c\uff0c\u8fd9\u4e5f\u662f&lt;code&gt;ThreadLocal&lt;\/code&gt;\u53d8\u91cf\u540d\u7684\u7531\u6765\u3002\u4ed6\u4eec\u53ef\u4ee5\u4f7f\u7528 &lt;code&gt;get\uff08\uff09&lt;\/code&gt; \u548c &lt;code&gt;set\uff08\uff09&lt;\/code&gt; \u65b9\u6cd5\u6765\u83b7\u53d6\u9ed8\u8ba4\u503c\u6216\u5c06\u5176\u503c\u66f4\u6539\u4e3a\u5f53\u524d\u7ebf\u7a0b\u6240\u5b58\u7684\u526f\u672c\u7684\u503c\uff0c\u4ece\u800c\u907f\u514d\u4e86\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002&lt;\/strong&gt;<\/p>\n<p>\u518d\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>\u6bd4\u5982\u6709\u4e24\u4e2a\u4eba\u53bb\u5b9d\u5c4b\u6536\u96c6\u5b9d\u7269\uff0c\u8fd9\u4e24\u4e2a\u5171\u7528\u4e00\u4e2a\u888b\u5b50\u7684\u8bdd\u80af\u5b9a\u4f1a\u4ea7\u751f\u4e89\u6267\uff0c\u4f46\u662f\u7ed9\u4ed6\u4eec\u4e24\u4e2a\u4eba\u6bcf\u4e2a\u4eba\u5206\u914d\u4e00\u4e2a\u888b\u5b50\u7684\u8bdd\u5c31\u4e0d\u4f1a\u51fa\u73b0\u8fd9\u6837\u7684\u95ee\u9898\u3002\u5982\u679c\u628a\u8fd9\u4e24\u4e2a\u4eba\u6bd4\u4f5c\u7ebf\u7a0b\u7684\u8bdd\uff0c\u90a3\u4e48ThreadLocal\u5c31\u662f\u7528\u6765\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u7ade\u4e89\u7684\u3002&lt;\/p&gt;<\/p>\n<p>&lt;h3&gt;ThreadLocal\u793a\u4f8b&lt;\/h3&gt;<\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u4e0a\u9762\u7684\u89e3\u91ca\uff0c\u5927\u5bb6\u5df2\u7ecf\u641e\u61c2 ThreadLocal \u7c7b\u662f\u4e2a\u4ec0\u4e48\u4e1c\u897f\u4e86\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;import java.text.SimpleDateFormat;import java.util.Random;public class ThreadLocalExample implements Runnable{     \/\/ SimpleDateFormat \u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u8981\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u526f\u672c    private static final ThreadLocal&lt;SimpleDateFormat&gt; formatter = ThreadLocal.withInitial(() -&gt; new SimpleDateFormat(&quot;&quot;yyyyMMdd HHmm&quot;&quot;));    public static void main(String[] args) throws InterruptedException {        ThreadLocalExample obj = new ThreadLocalExample();        for(int i=0 ; i&lt;10; i++){            Thread t = new Thread(obj, &quot;&quot;&quot;&quot;+i);            Thread.sleep(new Random().nextInt(1000));            t.start();        }    }    @Override    public void run() {        System.out.println(&quot;&quot;Thread Name= &quot;&quot;+Thread.currentThread().getName()+&quot;&quot; default Formatter = &quot;&quot;+formatter.get().toPattern());        try {            Thread.sleep(new Random().nextInt(1000));        } catch (InterruptedException e) {            e.printStackTrace();        }        \/\/formatter pattern is changed here by thread, but it won&#039;t reflect to other threads        formatter.set(new SimpleDateFormat());        System.out.println(&quot;&quot;Thread Name= &quot;&quot;+Thread.currentThread().getName()+&quot;&quot; formatter = &quot;&quot;+formatter.get().toPattern());    }}&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>Output:<\/p>\n<p>&lt;pre&gt;&lt;code&gt;Thread Name= 0 default Formatter = yyyyMMdd HHmmThread Name= 0 formatter = yy-M-d ah:mmThread Name= 1 default Formatter = yyyyMMdd HHmmThread Name= 2 default Formatter = yyyyMMdd HHmmThread Name= 1 formatter = yy-M-d ah:mmThread Name= 3 default Formatter = yyyyMMdd HHmmThread Name= 2 formatter = yy-M-d ah:mmThread Name= 4 default Formatter = yyyyMMdd HHmmThread Name= 3 formatter = yy-M-d ah:mmThread Name= 4 formatter = yy-M-d ah:mmThread Name= 5 default Formatter = yyyyMMdd HHmmThread Name= 5 formatter = yy-M-d ah:mmThread Name= 6 default Formatter = yyyyMMdd HHmmThread Name= 6 formatter = yy-M-d ah:mmThread Name= 7 default Formatter = yyyyMMdd HHmmThread Name= 7 formatter = yy-M-d ah:mmThread Name= 8 default Formatter = yyyyMMdd HHmmThread Name= 9 default Formatter = yyyyMMdd HHmmThread Name= 8 formatter = yy-M-d ah:mmThread Name= 9 formatter = yy-M-d ah:mm&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u4ece\u8f93\u51fa\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0cThread-0\u5df2\u7ecf\u6539\u53d8\u4e86formatter\u7684\u503c\uff0c\u4f46\u4ecd\u7136\u662fthread-2\u9ed8\u8ba4\u683c\u5f0f\u5316\u7a0b\u5e8f\u4e0e\u521d\u59cb\u5316\u503c\u76f8\u540c\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4e5f\u4e00\u6837\u3002<\/p>\n<p>\u4e0a\u9762\u6709\u4e00\u6bb5\u4ee3\u7801\u7528\u5230\u4e86\u521b\u5efa &lt;code&gt;ThreadLocal&lt;\/code&gt; \u53d8\u91cf\u7684\u90a3\u6bb5\u4ee3\u7801\u7528\u5230\u4e86 Java8 \u7684\u77e5\u8bc6\uff0c\u5b83\u7b49\u4e8e\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5982\u679c\u4f60\u5199\u4e86\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u7684\u8bdd\uff0cIDEA\u4f1a\u63d0\u793a\u4f60\u8f6c\u6362\u4e3aJava8\u7684\u683c\u5f0f(IDEA\u771f\u7684\u4e0d\u9519\uff01)\u3002\u56e0\u4e3aThreadLocal\u7c7b\u5728Java 8\u4e2d\u6269\u5c55\uff0c\u4f7f\u7528\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5&lt;code&gt;withInitial()&lt;\/code&gt;\uff0c\u5c06Supplier\u529f\u80fd\u63a5\u53e3\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt; private static final ThreadLocal&lt;SimpleDateFormat&gt; formatter = new ThreadLocal&lt;SimpleDateFormat&gt;(){        @Override        protected SimpleDateFormat initialValue()        {            return new SimpleDateFormat(&quot;&quot;yyyyMMdd HHmm&quot;&quot;);        }    };&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;h3&gt;&lt;\/h3&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; &lt;h3&gt;ThreadLocal\u7b80\u4ecb&lt;\/h3&gt; \u901a\u5e38\u60c5\u51b5 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-37486","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37486","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=37486"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37486\/revisions"}],"predecessor-version":[{"id":37487,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/37486\/revisions\/37487"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=37486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=37486"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=37486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}