一、场景
在java8以前,要格式化日期时间,就需要用到SimpleDateFormat 。
但我们知道SimpleDateFormat是线程不安全的,处理时要特别小心,要加锁或者不能定义为static,要在方法内new出对象,再进行格式化。很麻烦,而且重复地new出对象,也加大了内存开销。
后来Apache 在commons-lang 包中扩展了FastDateFormat对象,它是一个线程安全的,可以用来完美替换SimpleDateFormat。
二、SimpleDateFormat线程为什么是线程不安全的呢?
来看看SimpleDateFormat的源码
// Called from Format after creating a FieldDelegate
private StringBuffer format ( Date date, StringBuffer toAppendTo,
FieldDelegate delegate) {
// Convert input date to time field list
calendar. setTime ( date) ;
. . .
}
问题就出在成员变量calendar ,如果在使用SimpleDateFormat时,用static定义,那SimpleDateFormat变成了共享变量。那SimpleDateFormat中的calendar 就可以被多个线程访问到。
验证SimpleDateFormat线程不安全
public class SimpleDateFormatDemoTest {
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) ;
public static void main ( String [ ] args) {
//1、创建线程池
ExecutorService pool = Executors . newFixedThreadPool ( 5 ) ;
//2、为线程池分配任务
ThreadPoolTest threadPoolTest = new ThreadPoolTest ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
pool. submit ( threadPoolTest) ;
}
//3、关闭线程池
pool. shutdown ( ) ;
}
static class ThreadPoolTest implements Runnable {
private volatile int i= 0 ;
@Override
public void run ( ) {
while ( i< 10 ) {
String dateString = simpleDateFormat. format ( new Date ( ) ) ;
try {
Date parseDate = simpleDateFormat. parse ( dateString) ;
String dateString2 = simpleDateFormat. format ( parseDate) ;
System . out. println ( Thread . currentThread ( ) . getName ( ) + " : " + i++ ) ;
System . out. println ( dateString. equals ( dateString2) ) ;
System . out. println ( "-------------------------" ) ;
} catch ( ParseException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
出现了两次false,说明线程是不安全的。
三、FastDateFormat源码分析
Apache Commons Lang 3.5
//FastDateFormat
@Override
public String format ( final Date date) {
return printer. format ( date) ;
}
@Override
public String format ( final Date date) {
final Calendar c = Calendar . getInstance ( timeZone, locale) ;
c. setTime ( date) ;
return applyRulesToString ( c) ;
}
源码中 Calender 是在 format 方法里创建的,肯定不会出现 setTime 的线程安全问题。这样线程安全疑惑解决了。那还有性能问题要考虑?
我们来看下FastDateFormat是怎么获取的
FastDateFormat . getInstance ( ) ;
FastDateFormat . getInstance ( CHINESE_DATE_TIME_PATTERN) ;
看下对应的源码
/**
* 获得 FastDateFormat实例,使用默认格式和地区
*
* @return FastDateFormat
*/
public static FastDateFormat getInstance ( ) {
return CACHE. getInstance ( ) ;
}
/**
* 获得 FastDateFormat 实例,使用默认地区<br>
* 支持缓存
*
* @param pattern 使用{@link java.text.SimpleDateFormat} 相同的日期格式
* @return FastDateFormat
* @throws IllegalArgumentException 日期格式问题
*/
public static FastDateFormat getInstance ( final String pattern) {
return CACHE. getInstance ( pattern, null , null ) ;
}
这里有用到一个CACHE,看来用了缓存,往下看
private static final FormatCache < FastDateFormat > CACHE = new FormatCache < FastDateFormat > ( ) {
@Override
protected FastDateFormat createInstance ( final String pattern, final TimeZone timeZone, final Locale locale) {
return new FastDateFormat ( pattern, timeZone, locale) ;
}
} ;
//
abstract class FormatCache < F extends Format > {
. . .
private final ConcurrentMap < Tuple , F > cInstanceCache = new ConcurrentHashMap < > ( 7 ) ;
private static final ConcurrentMap < Tuple , String > C_DATE_TIME_INSTANCE_CACHE = new ConcurrentHashMap < > ( 7 ) ;
. . .
}
在getInstance 方法中加了ConcurrentMap 做缓存,提高了性能。且我们知道ConcurrentMap 也是线程安全的。
实践
/**
* 年月格式 {@link FastDateFormat}:yyyy-MM
*/
public static final FastDateFormat NORM_MONTH_FORMAT = FastDateFormat . getInstance ( NORM_MONTH_PATTERN) ;
//FastDateFormatpublic static FastDateFormat getInstance(final String pattern) { return CACHE.getInstance(pattern, null, null);}
如图可证,是使用了ConcurrentMap 做缓存。且key值是格式,时区和locale(语境)三者都相同为相同的key。
四、结论
java8之前,可使用FastDateFormat 替换SimpleDateFormat,达到线程安全的目的;
java8及以上的,java8提供了一套新的日期时间API,可以使用DateTimeFormatter来代替SimpleDateFormat。具体的源码分析,可以看这里,传送门:万字博文教你搞懂java源码的日期和时间相关用法