提交 ae0e5a94 authored 作者: noelgrandin@gmail.com's avatar noelgrandin@gmail.com

reduce synchronization bottleneck in processing date/time values by using a…

reduce synchronization bottleneck in processing date/time values by using a ThreadLocal for the cached Calendar object
上级 538252fb
...@@ -52,31 +52,22 @@ public class DateTimeUtils { ...@@ -52,31 +52,22 @@ public class DateTimeUtils {
private static final int[] DAYS_OFFSET = { 0, 31, 61, 92, 122, 153, 184, private static final int[] DAYS_OFFSET = { 0, 31, 61, 92, 122, 153, 184,
214, 245, 275, 306, 337, 366 }; 214, 245, 275, 306, 337, 366 };
private static int zoneOffset; private static final ThreadLocal<Calendar> cachedCalendar = new ThreadLocal<Calendar>() {
private static Calendar cachedCalendar; @Override
protected Calendar initialValue() {
return Calendar.getInstance();
}
};
private DateTimeUtils() { private DateTimeUtils() {
// utility class // utility class
} }
static {
getCalendar();
}
/** /**
* Reset the calendar, for example after changing the default timezone. * Reset the calendar, for example after changing the default timezone.
*/ */
public static void resetCalendar() { public static void resetCalendar() {
cachedCalendar = null; cachedCalendar.remove();
getCalendar();
}
private static Calendar getCalendar() {
if (cachedCalendar == null) {
cachedCalendar = Calendar.getInstance();
zoneOffset = cachedCalendar.get(Calendar.ZONE_OFFSET);
}
return cachedCalendar;
} }
/** /**
...@@ -388,16 +379,14 @@ public class DateTimeUtils { ...@@ -388,16 +379,14 @@ public class DateTimeUtils {
int millis) { int millis) {
Calendar c; Calendar c;
if (tz == TimeZone.getDefault()) { if (tz == TimeZone.getDefault()) {
c = getCalendar(); c = cachedCalendar.get();
} else { } else {
c = Calendar.getInstance(tz); c = Calendar.getInstance(tz);
} }
synchronized (c) { c.clear();
c.clear(); c.setLenient(lenient);
c.setLenient(lenient); setCalendarFields(c, year, month, day, hour, minute, second, millis);
setCalendarFields(c, year, month, day, hour, minute, second, millis); return c.getTime().getTime();
return c.getTime().getTime();
}
} }
private static void setCalendarFields(Calendar cal, int year, int month, private static void setCalendarFields(Calendar cal, int year, int month,
...@@ -427,18 +416,16 @@ public class DateTimeUtils { ...@@ -427,18 +416,16 @@ public class DateTimeUtils {
* @return the value * @return the value
*/ */
public static int getDatePart(java.util.Date d, int field) { public static int getDatePart(java.util.Date d, int field) {
Calendar c = getCalendar(); Calendar c = cachedCalendar.get();
synchronized (c) { c.setTime(d);
c.setTime(d); if (field == Calendar.YEAR) {
if (field == Calendar.YEAR) { return getYear(c);
return getYear(c); }
} int value = c.get(field);
int value = c.get(field); if (field == Calendar.MONTH) {
if (field == Calendar.MONTH) { return value + 1;
return value + 1;
}
return value;
} }
return value;
} }
/** /**
...@@ -463,7 +450,7 @@ public class DateTimeUtils { ...@@ -463,7 +450,7 @@ public class DateTimeUtils {
* @return the milliseconds * @return the milliseconds
*/ */
public static long getTimeLocalWithoutDst(java.util.Date d) { public static long getTimeLocalWithoutDst(java.util.Date d) {
return d.getTime() + zoneOffset; return d.getTime() + cachedCalendar.get().get(Calendar.ZONE_OFFSET);
} }
/** /**
...@@ -474,7 +461,7 @@ public class DateTimeUtils { ...@@ -474,7 +461,7 @@ public class DateTimeUtils {
* @return the number of milliseconds in UTC * @return the number of milliseconds in UTC
*/ */
public static long getTimeUTCWithoutDst(long millis) { public static long getTimeUTCWithoutDst(long millis) {
return millis - zoneOffset; return millis - cachedCalendar.get().get(Calendar.ZONE_OFFSET);
} }
/** /**
...@@ -744,12 +731,10 @@ public class DateTimeUtils { ...@@ -744,12 +731,10 @@ public class DateTimeUtils {
* @return the date value * @return the date value
*/ */
public static long dateValueFromDate(long ms) { public static long dateValueFromDate(long ms) {
Calendar cal = getCalendar(); Calendar cal = cachedCalendar.get();
synchronized (cal) { cal.clear();
cal.clear(); cal.setTimeInMillis(ms);
cal.setTimeInMillis(ms); return dateValueFromCalendar(cal);
return dateValueFromCalendar(cal);
}
} }
/** /**
...@@ -774,12 +759,10 @@ public class DateTimeUtils { ...@@ -774,12 +759,10 @@ public class DateTimeUtils {
* @return the nanoseconds * @return the nanoseconds
*/ */
public static long nanosFromDate(long ms) { public static long nanosFromDate(long ms) {
Calendar cal = getCalendar(); Calendar cal = cachedCalendar.get();
synchronized (cal) { cal.clear();
cal.clear(); cal.setTimeInMillis(ms);
cal.setTimeInMillis(ms); return nanosFromCalendar(cal);
return nanosFromCalendar(cal);
}
} }
/** /**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论