提交 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 {
private static final int[] DAYS_OFFSET = { 0, 31, 61, 92, 122, 153, 184,
214, 245, 275, 306, 337, 366 };
private static int zoneOffset;
private static Calendar cachedCalendar;
private static final ThreadLocal<Calendar> cachedCalendar = new ThreadLocal<Calendar>() {
@Override
protected Calendar initialValue() {
return Calendar.getInstance();
}
};
private DateTimeUtils() {
// utility class
}
static {
getCalendar();
}
/**
* Reset the calendar, for example after changing the default timezone.
*/
public static void resetCalendar() {
cachedCalendar = null;
getCalendar();
}
private static Calendar getCalendar() {
if (cachedCalendar == null) {
cachedCalendar = Calendar.getInstance();
zoneOffset = cachedCalendar.get(Calendar.ZONE_OFFSET);
}
return cachedCalendar;
cachedCalendar.remove();
}
/**
......@@ -388,17 +379,15 @@ public class DateTimeUtils {
int millis) {
Calendar c;
if (tz == TimeZone.getDefault()) {
c = getCalendar();
c = cachedCalendar.get();
} else {
c = Calendar.getInstance(tz);
}
synchronized (c) {
c.clear();
c.setLenient(lenient);
setCalendarFields(c, year, month, day, hour, minute, second, millis);
return c.getTime().getTime();
}
}
private static void setCalendarFields(Calendar cal, int year, int month,
int day, int hour, int minute, int second, int millis) {
......@@ -427,8 +416,7 @@ public class DateTimeUtils {
* @return the value
*/
public static int getDatePart(java.util.Date d, int field) {
Calendar c = getCalendar();
synchronized (c) {
Calendar c = cachedCalendar.get();
c.setTime(d);
if (field == Calendar.YEAR) {
return getYear(c);
......@@ -439,7 +427,6 @@ public class DateTimeUtils {
}
return value;
}
}
/**
* Get the year (positive or negative) from a calendar.
......@@ -463,7 +450,7 @@ public class DateTimeUtils {
* @return the milliseconds
*/
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 {
* @return the number of milliseconds in UTC
*/
public static long getTimeUTCWithoutDst(long millis) {
return millis - zoneOffset;
return millis - cachedCalendar.get().get(Calendar.ZONE_OFFSET);
}
/**
......@@ -744,13 +731,11 @@ public class DateTimeUtils {
* @return the date value
*/
public static long dateValueFromDate(long ms) {
Calendar cal = getCalendar();
synchronized (cal) {
Calendar cal = cachedCalendar.get();
cal.clear();
cal.setTimeInMillis(ms);
return dateValueFromCalendar(cal);
}
}
/**
* Calculate the date value from a given calendar.
......@@ -774,13 +759,11 @@ public class DateTimeUtils {
* @return the nanoseconds
*/
public static long nanosFromDate(long ms) {
Calendar cal = getCalendar();
synchronized (cal) {
Calendar cal = cachedCalendar.get();
cal.clear();
cal.setTimeInMillis(ms);
return nanosFromCalendar(cal);
}
}
/**
* Calculate the nanoseconds since midnight from a given calendar.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论