My limited understanding of ThreadLocal is that it has resource leak issues. I gather this problem can be remedied through proper use of WeakReferences with ThreadLocal (although I may have misunderstood this point.) I would simply like a pattern or example for correctly using ThreadLocal with WeakReference, if one exists. For instance, in this code snippet where would the WeakReference be introduced?
static class DateTimeFormatter {
private static final ThreadLocal<SimpleDateFormat> DATE_PARSER_THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() {
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy/MM/dd HH:mmz");
}
};
public String format(final Date date) {
return DATE_PARSER_THREAD_LOCAL.get().format(date);
}
public Date parse(final String date) throws ParseException
{
return DATE_PARSER_THREAD_LOCAL.get().parse(date);
}
}