Java Reference
In-Depth Information
DateFormat.MEDIUM).parse(str);
}
}
Compliant Solution (Synchronization)
This compliant solution makes DateHandler thread-safe by synchronizing statements
within the parse() method [API 2013]:
Click here to view code image
final class DateHandler {
private static DateFormat format =
DateFormat.getDateInstance(DateFormat.MEDIUM);
public static java.util.Date parse(String str)
throws ParseException {
synchronized (format) {
return format.parse(str);
}
}
}
Compliant Solution ( ThreadLocal Storage)
This compliant solution uses a ThreadLocal object to create a separate DateFormat in-
stance per thread:
Click here to view code image
final class DateHandler {
private static final ThreadLocal<DateFormat> format =
new ThreadLocal<DateFormat>() {
@Override protected DateFormat initialValue() {
return DateFormat.getDateInstance(DateFormat.MEDIUM);
}
};
// ...
}
Search WWH ::




Custom Search