I was horrified to see many of our app threads competing to synchronize on a java.util.Hashtable.get(xx) method that gets accessed from the Calendar's constructor.
at java.util.Hashtable.get(java.lang.Object)
at java.util.Calendar.setWeekCountData(java.util.Locale)
at java.util.Calendar.<init>(java.util.TimeZone, java.util.Locale)
at java.util.GregorianCalendar.<init>(java.util.TimeZone, java.util.Locale)
The ctor looks up a static hashtable that is meant to be serve as a cache, but ends up blocking all threads.
/**
* Cache to hold the firstDayOfWeek and minimalDaysInFirstWeek
* of a Locale.
*/
private static Hashtable<Locale, int[]> cachedLocaleData = new Hashtable<Locale, int[]>(3);
protected Calendar(TimeZone zone, Locale aLocale)
{
.. .. snip ..
setWeekCountData(aLocale);
}
private void setWeekCountData(Locale desiredLocale)
{
/* try to get the Locale data from the cache */
int[] data = cachedLocaleData.get(desiredLocale);
....
}
Is there a better way to manipulate dates? Does Joda bypass all these issues?
Better solution, use Java 7, the Hashtable
has been replaced by a ConcurrentMap
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With