From a06bd9c38e7230c9bca89cb121489471b8d77bf8 Mon Sep 17 00:00:00 2001 From: liquanyu Date: Wed, 26 Jul 2017 17:22:40 +0800 Subject: [PATCH] update code --- .../com/lyms/platform/msg/utils/DateUtils.java | 23 ++++++++-------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java b/platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java index fc7fa74..8c2f174 100644 --- a/platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java +++ b/platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java @@ -31,6 +31,9 @@ public class DateUtils { public static final String Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss"; public static final String Y_M_D_H_M = "yyyy-MM-dd HH:mm"; + /** 锁对象 */ + private static final Object lockObj = new Object(); + private static Map> formatMap = new HashMap>(); // private static ThreadLocal threadLocal = new ThreadLocal(){ @@ -68,34 +71,24 @@ public class DateUtils { */ private static SimpleDateFormat getDateFormat(final String dateFormat) { - ThreadLocal tl = formatMap.get(dateFormat); if (tl == null) { - try { - lock.lock(); + synchronized (lockObj) { tl = formatMap.get(dateFormat); - if (tl == null) - { - tl = new ThreadLocal() - { + if (tl == null) { + tl = new ThreadLocal() { @Override - protected SimpleDateFormat initialValue() - { + protected SimpleDateFormat initialValue() { return new SimpleDateFormat(dateFormat); } }; formatMap.put(dateFormat, tl); } } - finally - { - lock.unlock(); - } + } return tl.get(); - - } /** -- 1.8.3.1