{"id":10008115,"date":"2023-08-18T13:01:59","date_gmt":"2023-08-18T05:01:59","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008115"},"modified":"2023-08-18T13:01:59","modified_gmt":"2023-08-18T05:01:59","slug":"simpledateformat-%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8%e9%97%ae%e9%a2%98%e4%bf%ae%e5%a4%8d%e6%96%b9%e6%a1%88-%e4%ba%ac%e4%b8%9c%e7%89%a9%e6%b5%81%e6%8a%80%e6%9c%af%e5%9b%a2%e9%98%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008115","title":{"rendered":"SimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u4fee\u590d\u65b9\u6848 | \u4eac\u4e1c\u7269\u6d41\u6280\u672f\u56e2\u961f"},"content":{"rendered":"<h2>\u95ee\u9898\u4ecb\u7ecd<\/h2>\n<p>\u5728\u65e5\u5e38\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e0d\u53ef\u907f\u514d\u5730\u4f1a\u4f7f\u7528\u5230 JDK8 \u4e4b\u524d\u7684 Date \u7c7b\uff0c\u5728\u683c\u5f0f\u5316\u65e5\u671f\u6216\u89e3\u6790\u65e5\u671f\u65f6\u5c31\u9700\u8981\u7528\u5230 SimpleDateFormat \u7c7b\uff0c\u4f46\u7531\u4e8e\u8be5\u7c7b\u5e76\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u5e38\u53d1\u73b0\u5bf9\u8be5\u7c7b\u7684\u4e0d\u6070\u5f53\u4f7f\u7528\u4f1a\u5bfc\u81f4\u65e5\u671f\u89e3\u6790\u5f02\u5e38\uff0c\u4ece\u800c\u5f71\u54cd\u7ebf\u4e0a\u670d\u52a1\u53ef\u7528\u7387\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5bf9 SimpleDateFormat \u7c7b\u4e0d\u6070\u5f53\u4f7f\u7528\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code>package com.jd.threadsafe;\n\nimport java.text.SimpleDateFormat;\nimport java.util.concurrent.atomic.AtomicBoolean;\n\n\/**\n * @Date: 2023\/7\/25 10:47\n * @Desc: SimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u590d\u73b0\n * @Version: V1.0\n **\/\npublic class SimpleDateFormatTest {\n    private static final AtomicBoolean STOP = new AtomicBoolean();\n    private static final SimpleDateFormat FORMATTER = new SimpleDateFormat(&quot;yyyy-M-d&quot;); \/\/ \u975e\u7ebf\u7a0b\u5b89\u5168\n\n    public static void main(String[] args) {\n        Runnable runnable = () -&gt; {\n            int count = 0;\n            while (!STOP.get()) {\n                try {\n                    FORMATTER.parse(&quot;2023-7-15&quot;);\n                } catch (Exception e) {\n                    e.printStackTrace();\n                    if (++count &gt; 3) {\n                        STOP.set(true);\n                    }\n                }\n            }\n        };\n\n        new Thread(runnable).start();\n        new Thread(runnable).start();\n    }\n}\n\n\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6a21\u62df\u4e86\u591a\u7ebf\u7a0b\u5e76\u53d1\u4f7f\u7528 SimpleDateFormat \u5b9e\u4f8b\u7684\u573a\u666f\uff0c\u6b64\u65f6\u53ef\u89c2\u5bdf\u5230\u5982\u4e0b\u5f02\u5e38\u8f93\u51fa\uff1a<\/p>\n<pre><code>java.lang.NumberFormatException: For input string: &quot;&quot;\n    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)\n    at java.lang.Long.parseLong(Long.java:601)\n    at java.lang.Long.parseLong(Long.java:631)\n    at java.text.DigitList.getLong(DigitList.java:195)\n    at java.text.DecimalFormat.parse(DecimalFormat.java:2082)\n    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)\n    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)\n    at java.text.DateFormat.parse(DateFormat.java:364)\n    at com.jd.threadsafe.SimpleDateFormatTest.lambda$main$0(SimpleDateFormatTest.java:21)\n    at java.lang.Thread.run(Thread.java:750)\njava.lang.NumberFormatException: For input string: &quot;&quot;\n    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)\n    at java.lang.Long.parseLong(Long.java:601)\n    at java.lang.Long.parseLong(Long.java:631)\n    at java.text.DigitList.getLong(DigitList.java:195)\n    at java.text.DecimalFormat.parse(DecimalFormat.java:2082)\n    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162)\n    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)\n    at java.text.DateFormat.parse(DateFormat.java:364)\n    at com.jd.threadsafe.SimpleDateFormatTest.lambda$main$0(SimpleDateFormatTest.java:21)\n    at java.lang.Thread.run(Thread.java:750)\njava.lang.NumberFormatException: multiple points\n    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)\n    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)\n    at java.lang.Double.parseDouble(Double.java:538)\n    at java.text.DigitList.getDouble(DigitList.java:169)\n    at java.text.DecimalFormat.parse(DecimalFormat.java:2087)\n    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)\n    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)\n    at java.text.DateFormat.parse(DateFormat.java:364)\n    at com.jd.threadsafe.SimpleDateFormatTest.lambda$main$0(SimpleDateFormatTest.java:21)\n    at java.lang.Thread.run(Thread.java:750)\njava.lang.NumberFormatException: multiple points\n    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)\n    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)\n    at java.lang.Double.parseDouble(Double.java:538)\n    at java.text.DigitList.getDouble(DigitList.java:169)\n    at java.text.DecimalFormat.parse(DecimalFormat.java:2087)\n    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)\n    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)\n    at java.text.DateFormat.parse(DateFormat.java:364)\n    at com.jd.threadsafe.SimpleDateFormatTest.lambda$main$0(SimpleDateFormatTest.java:21)\n    at java.lang.Thread.run(Thread.java:750)\njava.lang.NumberFormatException: For input string: &quot;&quot;\n    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)\n    at java.lang.Long.parseLong(Long.java:601)\n    at java.lang.Long.parseLong(Long.java:631)\n    at java.text.DigitList.getLong(DigitList.java:195)\n    at java.text.DecimalFormat.parse(DecimalFormat.java:2082)\n    at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)\n    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)\n    at java.text.DateFormat.parse(DateFormat.java:364)\n    at com.jd.threadsafe.SimpleDateFormatTest.lambda$main$0(SimpleDateFormatTest.java:21)\n    at java.lang.Thread.run(Thread.java:750)\n\n\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u5f02\u5e38\u7684\u6839\u672c\u539f\u56e0\u662f\u56e0\u4e3a SimpleDateFormat \u662f<strong>\u6709\u72b6\u6001<\/strong>\u7684\uff0c\u5982 SimpleDateFormat \u7c7b\u4e2d\u542b\u6709<strong>\u975e\u7ebf\u7a0b\u5b89\u5168\u7684<\/strong> NumberFormat \u6210\u5458\u53d8\u91cf\uff1a<\/p>\n<pre><code>\/**\n * The number formatter that &lt;code&gt;DateFormat&lt;\/code&gt; uses to format numbers\n * in dates and times.  Subclasses should initialize this to a number format\n * appropriate for the locale associated with this &lt;code&gt;DateFormat&lt;\/code&gt;.\n * @serial\n *\/\nprotected NumberFormat numberFormat;\n\n\n<\/code><\/pre>\n<p>\u4ece NumberFormat \u7684 <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=c%2BJ6ZM8jP%2Fm3MM1496leRQ%3D%3D.1KN9zSuKqvm%2BCj4iQpERb147blhyG6%2BgJlSdGhPnBWQ%2Frdk8SMWlFswrBCI3HO8JBSZ%2BbLqbdgRliB3IqbVeb%2F5KhMlUY2l429UKJZbth08%3D\" rel=\"noopener noreferrer\">Java Doc<\/a> \u4e2d\u80fd\u770b\u5230\u5982\u4e0b\u63cf\u8ff0\uff1a<\/p>\n<blockquote><p>Synchronization Number formats are generally not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.<\/p><\/blockquote>\n<p>\u4ece SimpleDateFormat \u7684 <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=KyneS%2FpD2a3a7cmcup9GTQ%3D%3D.bPm%2BCfb2kSXIVfStZx9ljmKMIsLG15PrBjG7LxdvV1P11gZZKdeQ%2FszX895SFKt7ffPCPy7UmFUjrDnYYo%2Bvm%2FEJNn7k6sZzOKJBcVFMaD4%3D\" rel=\"noopener noreferrer\">Java Doc<\/a> \u4e2d\u80fd\u770b\u5230\u5982\u4e0b\u63cf\u8ff0\uff1a<\/p>\n<blockquote><p>Synchronization Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.<\/p><\/blockquote>\n<h2>\u4fee\u590d\u65b9\u6848\u4e00\uff1a\u52a0\u9501\uff08\u4e0d\u63a8\u8350\uff09<\/h2>\n<pre><code>package com.jd.threadsafe;\n\nimport java.text.SimpleDateFormat;\nimport java.util.concurrent.atomic.AtomicBoolean;\n\n\/**\n * @Date: 2023\/7\/25 10:47\n * @Desc: SimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u4fee\u590d\u65b9\u6848\uff1a\u52a0\u9501\n * @Version: V1.0\n **\/\npublic class SimpleDateFormatLockTest {\n    private static final AtomicBoolean STOP = new AtomicBoolean();\n    private static final SimpleDateFormat FORMATTER = new SimpleDateFormat(&quot;yyyy-M-d&quot;); \/\/ \u975e\u7ebf\u7a0b\u5b89\u5168\n\n    public static void main(String[] args) {\n        Runnable runnable = () -&gt; {\n            int count = 0;\n            while (!STOP.get()) {\n                try {\n                    synchronized (FORMATTER) {\n                        FORMATTER.parse(&quot;2023-7-15&quot;);\n                    }\n                } catch (Exception e) {\n                    e.printStackTrace();\n                    if (++count &gt; 3) {\n                        STOP.set(true);\n                    }\n                }\n            }\n        };\n\n        new Thread(runnable).start();\n        new Thread(runnable).start();\n    }\n\n}\n\n\n<\/code><\/pre>\n<p>\u9996\u5148\u6211\u4eec\u80fd\u60f3\u5230\u7684\u6700\u7b80\u5355\u7684\u89e3\u51b3\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u7684\u4fee\u590d\u65b9\u6848\u5373\u52a0\u9501\uff0c\u5982\u4ee5\u4e0a\u4fee\u590d\u65b9\u6848\uff0c\u4f7f\u7528 synchronized \u5173\u952e\u5b57\u5bf9 FORMATTER \u5b9e\u4f8b\u8fdb\u884c\u52a0\u9501\uff0c\u6b64\u65f6\u591a\u7ebf\u7a0b\u8fdb\u884c\u65e5\u671f\u683c\u5f0f\u5316\u65f6\u9000\u5316\u4e3a\u4e32\u884c\u6267\u884c\uff0c\u4fdd\u8bc1\u4e86<strong>\u6b63\u786e\u6027<\/strong>\u4f46<strong>\u727a\u7272\u4e86\u6027\u80fd<\/strong>\uff0c\u4e0d\u63a8\u8350\u3002<\/p>\n<h2>\u4fee\u590d\u65b9\u6848\u4e8c\uff1a\u6808\u5c01\u95ed\uff08\u4e0d\u63a8\u8350\uff09<\/h2>\n<p>\u5982\u679c\u6309\u7167\u6587\u6863\u4e2d\u7684\u63a8\u8350\u7528\u6cd5\uff0c\u53ef\u77e5\u63a8\u8350\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u521b\u5efa\u72ec\u7acb\u7684 SimpleDateFormat \u5b9e\u4f8b\uff0c\u4e00\u79cd\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\u5728\u65b9\u6cd5\u8c03\u7528\u65f6\u6bcf\u6b21\u521b\u5efa SimpleDateFormat \u5b9e\u4f8b\uff0c\u4ee5\u5b9e\u73b0<strong>\u6808\u5c01\u95ed<\/strong>\u7684\u6548\u679c\uff0c\u5982\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code>package com.jd.threadsafe;\n\nimport java.text.SimpleDateFormat;\nimport java.util.concurrent.atomic.AtomicBoolean;\n\n\/**\n * @Date: 2023\/7\/25 10:47\n * @Desc: SimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u4fee\u590d\u65b9\u6848\uff1a\u6808\u5c01\u95ed\n * @Version: V1.0\n **\/\npublic class SimpleDateFormatStackConfinementTest {\n    private static final AtomicBoolean STOP = new AtomicBoolean();\n\n    public static void main(String[] args) {\n        Runnable runnable = () -&gt; {\n            int count = 0;\n            while (!STOP.get()) {\n                try {\n                    new SimpleDateFormat(&quot;yyyy-M-d&quot;).parse(&quot;2023-7-15&quot;);\n                } catch (Exception e) {\n                    e.printStackTrace();\n                    if (++count &gt; 3) {\n                        STOP.set(true);\n                    }\n                }\n            }\n        };\n\n        new Thread(runnable).start();\n        new Thread(runnable).start();\n    }\n\n}\n\n\n<\/code><\/pre>\n<p>\u5373\u5c06\u5171\u7528\u7684 SimpleDateFormat \u5b9e\u4f8b\u8c03\u6574\u4e3a\u6bcf\u6b21\u521b\u5efa\u65b0\u7684\u5b9e\u4f8b\uff0c\u8be5\u4fee\u590d\u65b9\u6848\u4fdd\u8bc1\u4e86\u6b63\u786e\u6027\u4f46\u6bcf\u6b21\u65b9\u6cd5\u8c03\u7528\u9700\u8981\u521b\u5efa SimpleDateFormat \u5b9e\u4f8b\uff0c\u5e76\u672a\u590d\u7528 SimpleDateFormat \u5b9e\u4f8b\uff0c<strong>\u5b58\u5728 GC \u635f\u8017<\/strong>\uff0c\u6240\u4ee5\u5e76\u4e0d\u63a8\u8350\u3002<\/p>\n<h2>\u4fee\u590d\u65b9\u6848\u4e09\uff1aThreadLocal\uff08\u63a8\u8350\uff09<\/h2>\n<p>\u5982\u679c\u65e5\u671f\u683c\u5f0f\u5316\u64cd\u4f5c\u662f\u5e94\u7528\u91cc\u7684\u9ad8\u9891\u64cd\u4f5c\uff0c\u4e14\u9700\u8981\u4f18\u5148\u4fdd\u8bc1\u6027\u80fd\uff0c\u90a3\u4e48\u5efa\u8bae\u6bcf\u4e2a\u7ebf\u7a0b\u590d\u7528 SimpleDateFormat \u5b9e\u4f8b\uff0c\u6b64\u65f6\u53ef\u5f15\u5165 ThreadLocal \u7c7b\u6765\u89e3\u51b3\u8be5\u95ee\u9898\uff1a<\/p>\n<pre><code>package com.jd.threadsafe;\n\nimport java.text.SimpleDateFormat;\nimport java.util.concurrent.atomic.AtomicBoolean;\n\n\/**\n * @Date: 2023\/7\/25 10:47\n * @Desc: SimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u4fee\u590d\u65b9\u6848\uff1aThreadLocal\n * @Version: V1.0\n **\/\npublic class SimpleDateFormatThreadLocalTest {\n    private static final AtomicBoolean STOP = new AtomicBoolean();\n    private static final ThreadLocal&lt;SimpleDateFormat&gt; SIMPLE_DATE_FORMAT_THREAD_LOCAL = ThreadLocal.withInitial(() -&gt; new SimpleDateFormat(&quot;yyyy-M-d&quot;));\n\n    public static void main(String[] args) {\n        Runnable runnable = () -&gt; {\n            int count = 0;\n            while (!STOP.get()) {\n                try {\n                    SIMPLE_DATE_FORMAT_THREAD_LOCAL.get().parse(&quot;2023-7-15&quot;);\n                } catch (Exception e) {\n                    e.printStackTrace();\n                    if (++count &gt; 3) {\n                        STOP.set(true);\n                    }\n                }\n            }\n        };\n\n        new Thread(runnable).start();\n        new Thread(runnable).start();\n    }\n\n}\n\n\n<\/code><\/pre>\n<p>\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4e0d\u4f1a\u518d\u89c2\u5bdf\u5230\u5f02\u5e38\u8f93\u51fa\uff0c\u56e0\u4e3a\u5df2\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u521b\u5efa\u4e86\u72ec\u7acb\u7684 SimpleDateFormat \u5b9e\u4f8b\uff0c\u5373\u5728<strong>\u7ebf\u7a0b\u7ef4\u5ea6<\/strong>\u590d\u7528\u4e86 SimpleDateFormat \u5b9e\u4f8b\uff0c\u5728\u7ebf\u7a0b\u6c60\u7b49\u6c60\u5316\u573a\u666f\u4e0b\u76f8\u6bd4\u4e0a\u65b9<strong>\u6808\u5c01\u95ed<\/strong>\u7684\u4fee\u590d\u65b9\u6848\u964d\u4f4e\u4e86 GC \u635f\u8017\uff0c\u540c\u65f6\u4e5f\u89c4\u907f\u4e86\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u3002<\/p>\n<p>\u4ee5\u4e0a\u4f7f\u7528 ThreadLocal \u5728\u7ebf\u7a0b\u7ef4\u5ea6\u590d\u7528\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u5b9e\u4f8b\u53ef\u8ba4\u4e3a\u662f\u4e00\u79cd\u901a\u7528\u7684<strong>\u6a21\u5f0f<\/strong>\uff0c\u53ef\u5728 JDK \u53ca\u4e0d\u5c11\u5f00\u6e90\u9879\u76ee\u4e2d\u770b\u5230\u7c7b\u4f3c\u7684\u6a21\u5f0f\u5b9e\u73b0\uff0c\u5982\u5728 JDK \u6700\u5e38\u89c1\u7684 String \u7c7b\u4e2d\uff0c\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7f16\u89e3\u7801\u6240\u9700\u8981\u7528\u5230\u7684 StringDecoder \u53ca StringEncoder \u5373\u4f7f\u7528\u4e86 ThreadLocal \u6765\u89c4\u907f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff1a<\/p>\n<pre><code>\/**\n * Utility class for string encoding and decoding.\n *\/\nclass StringCoding {\n\n    private StringCoding() { }\n\n    \/** The cached coders for each thread *\/\n    private final static ThreadLocal&lt;SoftReference&lt;StringDecoder&gt;&gt; decoder =\n        new ThreadLocal&lt;&gt;();\n    private final static ThreadLocal&lt;SoftReference&lt;StringEncoder&gt;&gt; encoder =\n        new ThreadLocal&lt;&gt;();\n\n    \/\/ ...\n}\n\n\n<\/code><\/pre>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=owFloLWrIjM4%2F5o7AwKt5w%3D%3D.w18TZVRL%2BXfIPxilx1fV%2FjzA2GpzPGW3R72%2BodaZ7iDb2KqnviM4GF3jd%2FSWFVAdEUpId15bIdKnCZITtO7m491aIfqSK7ti%2FC%2BBxaK%2FjiCxfvmiwa1n9XkzGThU6V%2FrEqzSC17KJ9WLDQqIbzv3zQ%3D%3D\" rel=\"noopener noreferrer\">JDK8 &#8211; StringCoding<\/a><\/p>\n<p>\u5728 Dubbo \u7684 ThreadLocalKryoFactory \u7c7b\u4e2d\uff0c\u5728\u5bf9\u975e\u7ebf\u7a0b\u5b89\u5168\u7c7b Kryo \u7684\u4f7f\u7528\u4e2d\uff0c\u4e5f\u4f7f\u7528\u4e86 ThreadLocal \u7c7b\u6765\u89c4\u907f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff1a<\/p>\n<pre><code>package org.apache.dubbo.common.serialize.kryo.utils;\n\nimport com.esotericsoftware.kryo.Kryo;\n\npublic class ThreadLocalKryoFactory extends AbstractKryoFactory {\n\n    private final ThreadLocal&lt;Kryo&gt; holder = new ThreadLocal&lt;Kryo&gt;() {\n        @Override\n        protected Kryo initialValue() {\n            return create();\n        }\n    };\n\n    @Override\n    public void returnKryo(Kryo kryo) {\n        \/\/ do nothing\n    }\n\n    @Override\n    public Kryo getKryo() {\n        return holder.get();\n    }\n}\n\n\n<\/code><\/pre>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=bG%2BO6r7AO4TZqR6JA%2BRuYw%3D%3D.oCWkVqrs28VlEnK%2Ft6Z1FFiNQY0Z%2BAMD3RJEuANAXJdWy2acmi3xz83GCUwzt0qFL4OaAzCNIrWy%2F0HK8JEdirM%2FOWte55vXmZFV8I4zt6KzxJKrfQXYwW%2FfZy0bCGu6k93BfMRXcAd7rAkkY6Db2f738qjbi3lhpeGJirapCrzge36oV1N8yGoWeyI82sGiI%2BGh3zCTuL4Bep6XCUKQvFkMC9KysZMS5wjRN4Bft1csGuDLT7wnSB97hYGM3m%2Bq\" rel=\"noopener noreferrer\">Dubbo &#8211; ThreadLocalKryoFactory<\/a><\/p>\n<p>\u7c7b\u4f3c\u5730\uff0c\u5728 HikariCP \u7684 <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=qKGWMUgSSQLbpATLWG4PlQ%3D%3D.uZy9vhXvx6Vh5irAWElNCuOrdfpmrFFhhMP1yycHSWZ0SypGVOONdYjEy45YQG9QrSp1n6Q6NUF%2BVKnnR72zW8B0KQHVh9KU2KxIbpwh1%2B6LUGghemeaIXHs9DQLFmiFnYKeLt%2BsqWsNWJqi2BCB8BDPlKc3z7uOz%2F5czyUl9qo%3D\" rel=\"noopener noreferrer\">ConcurrentBag<\/a> \u7c7b\u4e2d\uff0c\u4e5f\u7528\u5230\u4e86 ThreadLocal \u7c7b\u6765\u89c4\u907f\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0c\u6b64\u5904\u4e0d\u518d\u8fdb\u4e00\u6b65\u5c55\u5f00\u3002<\/p>\n<h2>\u4fee\u590d\u65b9\u6848\u56db\uff1aFastDateFormat\uff08\u63a8\u8350\uff09<\/h2>\n<p>\u9488\u5bf9 SimpleDateFormat \u7c7b\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff0capache commons-lang \u63d0\u4f9b\u4e86 FastDateFormat \u7c7b\u3002\u5176\u90e8\u5206 <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=qW%2BYIAqYOzr58%2FPftadhww%3D%3D.hfp%2ByMnb%2FTe9cADXF03bnZkkivR5AMfjyOfa8zTKcmDH5XaLVul8O1aNRboMW%2F01RniiBIHSrqxcJPAFiSz%2Fd4hmM%2BReNc99L70Yt9QeSAiC1J3LiCrhV%2BsRLSG%2F1sHlU7iRd6p4rMBSJFYKi537qw%3D%3D\" rel=\"noopener noreferrer\">Java Doc<\/a> \u5982\u4e0b\uff1a<\/p>\n<blockquote><p>FastDateFormat is a fast and thread-safe version of<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=7jG4eaDkD16Ktuou5EvQnA%3D%3D.IuJ6W8%2Fh8N5h81qUbvVL677OvY2zmTeU7VLpWCnsrHTD2RKN5LAMRQswjaR%2FEcMV4KZ6wygJAMBDxWeHq9zPI3Lqlh5506mEyr%2FyJiEk6x37mjvNCJX5HqGrIeG6jIpU\" rel=\"noopener noreferrer\"><code>SimpleDateFormat<\/code><\/a>. To obtain an instance of FastDateFormat, use one of the static factory methods:<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=lavsRV0pypbBeNtDhU%2B8Fw%3D%3D.VRqbZI0cS5B0IhBrWZcdYHB%2Bqkf3bvViUSnebnHrxoqK%2FSAa9t6U1hRVHXqAbyystP6mpaV%2BMpqI7Q2J878HVQi%2FD08mOBZbqu%2BfIpv%2BYVl9691cTvKpwnefF8gLHH2f36aLjPm%2Fr3PZ0dXRWf2oYfbv3OosBU0c7JFHbn2YTqjnjvHhfBZhLtDnll6Gs7pHhtuge%2FzeiBWaHeiCmLR9YrVYOtYIzLvZJsmYGOOHvvQ%3D\" rel=\"noopener noreferrer\"><code>getInstance(String, TimeZone, Locale)<\/code><\/a>,<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=mDY8xXNdeD%2BAi4VvoUHW%2FA%3D%3D.lnW4QEcpmRNzuSagyS2m5DV%2BEg2SMS30OQS6SpFRSNJ%2BnTNmbqHAQF38X52%2B3yWzo2RGP%2Bi14joSFF93RkhGSiM3svE9cLnjqVo7kmrdNIkwiLxHgr0%2B6E9Y7xxHni6h%2B8LTJ4m4L0ZRiWYkqWvBKVo6F2oUXTJ0mDPDKqEoiCj%2BdvtcmwEsxj9fIWMDeudfdqg%2BoQDa53FkyFEKo13hCiJ7gVw6ObOVL3CEpikAUWI%3D\" rel=\"noopener noreferrer\"><code>getDateInstance(int, TimeZone, Locale)<\/code><\/a>,<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=dIi5CTEHBt8OPpW%2BkkyHzg%3D%3D.loj1H6uyJ2KiQmQZDb16U%2BDd0rP5g1GSZibmhrXAZs7yBgFP7IOsPa7kN3W%2BagKqzfxNR6o0lOAhYNffMcrMAe8%2FGqGYCnXqlbAIQbLloJwbb%2B1YSG6QdC5BlfsWcjLXFnUaBQXo6sZ26duXF9%2B%2B0bPmz9nMimwjBLZkMMpj%2BspvS9I9TJ%2Bs5ZiEGqzvlGDCrOr9V7YzFicJ5djJ0IyxJB9zV9FCCbSmvXMS%2Bmt5%2FdY%3D\" rel=\"noopener noreferrer\"><code>getTimeInstance(int, TimeZone, Locale)<\/code><\/a>, or<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=b3mg2dSAOJO6tEFnxjuJDg%3D%3D.DWeoUatIYROi2U8au38euSdJ8kZ3VisgBEOMewp93MIxy6gLPEGjIxQD1B6pxBKYobQvQDWVA27QABt%2BFRsRB%2F2oAvVc4RobAxvJHFJjo%2FYZCgCIZJ%2BCzZjCajhqFRxReZ%2BvaynK7v9LIJuDjLU7Wt8ebhGJJcJ2F%2FSREn2RNS939YFKuLwfHuFrUtOuLM4z%2F72oQWoBMi51onDYaZLAS8JdAZySqxD3ZzdVCDHUfjY%3D\" rel=\"noopener noreferrer\"><code>getDateTimeInstance(int, int, TimeZone, Locale)<\/code><\/a> Since FastDateFormat is thread safe, you can use a static member instance: private static final FastDateFormat DATE_FORMATTER = FastDateFormat.getDateTimeInstance(FastDateFormat.LONG, FastDateFormat.SHORT); This class can be used as a direct replacement to<code>SimpleDateFormat<\/code>in most formatting and parsing situations. This class is especially useful in multi-threaded server environments.<code>SimpleDateFormat<\/code>is not thread-safe in any JDK version, nor will it be as Sun have closed the bug\/RFE. All patterns are compatible with SimpleDateFormat (except time zones and some year patterns &#8211; see below).<\/p><\/blockquote>\n<p>\u8be5\u4fee\u590d\u65b9\u6848\u76f8\u5bf9\u6765\u8bf4\u4ee3\u7801\u6539\u9020\u6700\u5c0f\uff0c\u4ec5\u9700\u5728\u58f0\u660e\u9759\u6001 SimpleDateFormat \u5b9e\u4f8b\u4ee3\u7801\u5904\u5c06 SimpleDateFormat \u5b9e\u4f8b\u66ff\u6362\u4e3a FastDateFormat \u5b9e\u4f8b\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>package com.jd.threadsafe;\n\nimport org.apache.commons.lang3.time.FastDateFormat;\n\nimport java.util.concurrent.atomic.AtomicBoolean;\n\n\/**\n * @Date: 2023\/7\/6 20:05\n * @Desc: SimpleDateFormat \u7ebf\u7a0b\u5b89\u5168\u4fee\u590d\u65b9\u6848\uff1aFastDateFormat\n * @Version: V1.0\n **\/\npublic class FastDateFormatTest {\n    private static final AtomicBoolean STOP = new AtomicBoolean();\n    private static final FastDateFormat FORMATTER = FastDateFormat.getInstance(&quot;yyyy-M-d&quot;);\n\n    public static void main(String[] args) {\n        Runnable runnable = () -&gt; {\n            int count = 0;\n            while (!STOP.get()) {\n                try {\n                    FORMATTER.parse(&quot;2023-7-15&quot;);\n                } catch (Exception e) {\n                    e.printStackTrace();\n                    if (++count &gt; 3) {\n                        STOP.set(true);\n                    }\n                }\n            }\n        };\n\n        new Thread(runnable).start();\n        new Thread(runnable).start();\n    }\n\n}\n\n\n<\/code><\/pre>\n<p>\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4e0d\u4f1a\u518d\u89c2\u5bdf\u5230\u5f02\u5e38\u8f93\u51fa\uff0c\u56e0\u4e3a FastDateFormat \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u5b9e\u73b0\uff0c\u652f\u6301\u591a\u7ebf\u7a0b\u5e76\u53d1\u8c03\u7528\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u4fee\u590d\u65b9\u6848\uff0c\u90fd\u9700\u8981\u5728\u4fee\u6539\u540e\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\uff0c\u4fdd\u8bc1\u4fee\u590d\u540e\u4e0d\u5f71\u54cd\u539f\u6709\u4e1a\u52a1\u903b\u8f91\uff0c\u5982\u901a\u8fc7\u5355\u5143\u6d4b\u8bd5\u3001\u6d41\u91cf\u56de\u653e\u7b49\u65b9\u5f0f\u6765\u4fdd\u8bc1\u672c\u6b21\u4fee\u590d\u7684\u6b63\u786e\u6027\u3002<\/p>\n<h2>\u601d\u8003<\/h2>\n<p>\u4ee3\u7801\u91cc\u4f7f\u7528 SimpleDateFormat \u7c7b\u7684\u539f\u56e0\u662f\u56e0\u4e3a\u65e5\u671f\u4f7f\u7528\u4e86 Date \u7c7b\uff0c\u4e0e Date \u76f8\u914d\u5957\u7684 JDK \u683c\u5f0f\u5316\u7c7b\u5373 SimpleDateFormat \u7c7b\uff0c\u5982\u679c\u6211\u4eec\u5728\u5904\u7406\u65e5\u671f\u65f6\u4f7f\u7528 JDK8 \u5f15\u5165\u7684 LocalDateTime \u7b49<strong>\u4e0d\u53ef\u53d8<\/strong>\u65e5\u671f\u7c7b\uff0c\u90a3\u4e48\u683c\u5f0f\u5316\u5c06\u4f7f\u7528\u914d\u5957\u7684<strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\u7684 DateTimeFormatter \u7c7b\uff0c\u4ece\u6839\u6e90\u4e0a\u89c4\u907f\u6389\u5bf9\u975e\u7ebf\u7a0b\u5b89\u5168\u7c7b SimpleDateFormat \u7c7b\u7684\u4f7f\u7528\u3002<\/p>\n<blockquote>\n<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u7269\u6d41 \u5218\u5efa\u8bbe \u5f20\u4e5d\u9f99 \u7530\u723d<\/p>\n<p>\u6765\u6e90\uff1a\u4eac\u4e1c\u4e91\u5f00\u53d1\u8005\u793e\u533a \u81ea\u733f\u5176\u8bf4Tech \u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u6e90<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044126582\">https:\/\/segmentfault.com\/a\/1190000044126582<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u4ecb\u7ecd \u5728\u65e5\u5e38\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4e0d\u53ef\u907f\u514d\u5730\u4f1a\u4f7f\u7528\u5230 JDK8 \u4e4b\u524d\u7684 Date \u7c7b\uff0c\u5728\u683c\u5f0f\u5316\u65e5\u671f\u6216\u89e3\u6790\u65e5\u671f\u65f6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008115","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008115"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008115\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}