Better concurrency with the Java 8 API ConcurrentMap#computeIfAbsent().

This commit is contained in:
Gary Gregory 2022-03-20 14:06:39 -04:00
parent 89bcc5f80e
commit 5648bc42e2
1 changed files with 3 additions and 10 deletions

View File

@ -653,16 +653,9 @@ public class FastDateParser implements DateParser, Serializable {
*/
private Strategy getLocaleSpecificStrategy(final int field, final Calendar definingCalendar) {
final ConcurrentMap<Locale, Strategy> cache = getCache(field);
Strategy strategy = cache.get(locale);
if (strategy == null) {
strategy = field == Calendar.ZONE_OFFSET ? new TimeZoneStrategy(locale)
: new CaseInsensitiveTextStrategy(field, definingCalendar, locale);
final Strategy inCache = cache.putIfAbsent(locale, strategy);
if (inCache != null) {
return inCache;
}
}
return strategy;
return cache.computeIfAbsent(locale, k -> {
return field == Calendar.ZONE_OFFSET ? new TimeZoneStrategy(locale) : new CaseInsensitiveTextStrategy(field, definingCalendar, locale);
});
}
/**