[
由于SimpleDateForamt是非线程安全的,所以想用ThreadLocal封装一下,但是同事说这样写会造成内存泄露,要请教一下各位, 下面这样写会造成内存泄露么,烦请大伙给指点指点,不胜感激。(环境springmvc + spring +ibatis,使用了线程池)
public class DateUtil { private static ThreadLocal<DateFormat> sdf = new ThreadLocal<DateFormat>() { protected DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd"); }; }; public static DateFormat getDateFormat(ThreadLocal<DateFormat> tl) { return tl.get(); } public static String format(Date date) { if (date == null) { return ""; } return getDateFormat(sdf).format(date); } public static Date parse(String st) throws ParseException { return getDateFormat(sdf).parse(st); } }