Commit a06bd9c38e7230c9bca89cb121489471b8d77bf8
1 parent
f15b752b19
Exists in
master
and in
6 other branches
update code
Showing 1 changed file with 8 additions and 15 deletions
platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/DateUtils.java
View file @
a06bd9c
| ... | ... | @@ -31,6 +31,9 @@ |
| 31 | 31 | public static final String Y_M_D_H_M_S = "yyyy-MM-dd HH:mm:ss"; |
| 32 | 32 | public static final String Y_M_D_H_M = "yyyy-MM-dd HH:mm"; |
| 33 | 33 | |
| 34 | + /** 锁对象 */ | |
| 35 | + private static final Object lockObj = new Object(); | |
| 36 | + | |
| 34 | 37 | private static Map<String, ThreadLocal<SimpleDateFormat>> formatMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>(); |
| 35 | 38 | |
| 36 | 39 | // private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>(){ |
| 37 | 40 | |
| 38 | 41 | |
| 39 | 42 | |
| 40 | 43 | |
| 41 | 44 | |
| ... | ... | @@ -68,34 +71,24 @@ |
| 68 | 71 | */ |
| 69 | 72 | private static SimpleDateFormat getDateFormat(final String dateFormat) |
| 70 | 73 | { |
| 71 | - | |
| 72 | 74 | ThreadLocal<SimpleDateFormat> tl = formatMap.get(dateFormat); |
| 73 | 75 | if (tl == null) |
| 74 | 76 | { |
| 75 | - try { | |
| 76 | - lock.lock(); | |
| 77 | + synchronized (lockObj) { | |
| 77 | 78 | tl = formatMap.get(dateFormat); |
| 78 | - if (tl == null) | |
| 79 | - { | |
| 80 | - tl = new ThreadLocal<SimpleDateFormat>() | |
| 81 | - { | |
| 79 | + if (tl == null) { | |
| 80 | + tl = new ThreadLocal<SimpleDateFormat>() { | |
| 82 | 81 | @Override |
| 83 | - protected SimpleDateFormat initialValue() | |
| 84 | - { | |
| 82 | + protected SimpleDateFormat initialValue() { | |
| 85 | 83 | return new SimpleDateFormat(dateFormat); |
| 86 | 84 | } |
| 87 | 85 | }; |
| 88 | 86 | formatMap.put(dateFormat, tl); |
| 89 | 87 | } |
| 90 | 88 | } |
| 91 | - finally | |
| 92 | - { | |
| 93 | - lock.unlock(); | |
| 94 | - } | |
| 89 | + | |
| 95 | 90 | } |
| 96 | 91 | return tl.get(); |
| 97 | - | |
| 98 | - | |
| 99 | 92 | } |
| 100 | 93 | |
| 101 | 94 | /** |