Using Simple Date format is not thread safe. In order to avoid it, ThreadLocal is used. It is used to maintain the state in each thread. We should be careful while using it as it can lead to memory leaks. The SDF is set on the threadlocal and later used to read from it. To prevent memory leakage, it should be removed as well. Can you please illustrate the same with an example where the thread local needs to be removed after the SDF usage.
Below is the code
public class ConcurrentDateFormatAccess {
private ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat> () {
@Override
public DateFormat get() {
return super.get();
}
@Override
protected DateFormat initialValue() {
return new SimpleDateFormat("yyyy MM dd");
}
@Override
public void remove() {
super.remove();
}
@Override
public void set(DateFormat value) {
super.set(value);
}
};
public Date convertStringToDate(String dateString) throws ParseException {
return df.get().parse(dateString);
}
}
Please help with the client code which would be calling this and also handling the removal part of threadLocal as well