final MultipartKey key = new MultipartKey(pattern, timeZone, locale); F format = cInstanceCache.get(key); if (format == null) { format = createInstance(pattern, timeZone, locale); final F previousValue= cInstanceCache.putIfAbsent(key, format); if (previousValue != null) { // another thread snuck in and did the same work // we should return the instance that is in ConcurrentMap format= previousValue; } }
而在进行格式化的时候Calendar使用的是方法内部的局部变量,是不会出现线程安全问题的
1 2 3 4 5
public String format(final Date date){ final Calendar c = newCalendar(); c.setTime(date); return applyRulesToString(c); }