安全有效地提升simpleDateFormat性能
首先看下JDK原始文档SimpleDateFormat的描述:
Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.
因此SimpleDateFormat带来的严重的性能问题,创建一个 SimpleDateFormat实例的开销比较昂贵,解析字符串时间时频繁创建生命周期短暂的实例导致性能低下。即使将 SimpleDateFormat定义为静态类变量,貌似能解决这个问题,但是SimpleDateFormat是非线程安全的,同样存在问题,如果用 ‘synchronized’线程同步同样面临问题,同步导致性能下降(线程之间序列化的获取SimpleDateFormat实例)。
为什么创建一个simpleDateFormat会花费很大的代价了?
?
protected T initialValue() { return null;//直接返回null}?第三种方式:
?? apache commons-lang包的DateFormatUtils或者FastDateFormat实现,apache保证是线程安全的,并且更高效。
?
?
?
?