想必大家對(duì)SimpleDateFormat并不陌生笑窜。SimpleDateFormat 是 Java 中一個(gè)非常常用的類匕累,該類用來(lái)對(duì)日期字符串進(jìn)行解析和格式化輸出,但如果使用不小心會(huì)導(dǎo)致非常微妙和難以調(diào)試的問(wèn)題,因?yàn)?DateFormat 和 SimpleDateFormat 類不都是線程安全的雷袋,在多線程環(huán)境下調(diào)用 format() 和 parse() 方法應(yīng)該使用同步代碼來(lái)避免問(wèn)題。下面我們通過(guò)一個(gè)具體的場(chǎng)景來(lái)一步步的深入學(xué)習(xí)和理解SimpleDateFormat類辞居。
一.引子
我們都是優(yōu)秀的程序員楷怒,我們都知道在程序中我們應(yīng)當(dāng)盡量少的創(chuàng)建SimpleDateFormat 實(shí)例,因?yàn)閯?chuàng)建這么一個(gè)實(shí)例需要耗費(fèi)很大的代價(jià)瓦灶。在一個(gè)讀取數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)出到excel文件的例子當(dāng)中鸠删,每次處理一個(gè)時(shí)間信息的時(shí)候,就需要?jiǎng)?chuàng)建一個(gè)SimpleDateFormat實(shí)例對(duì)象贼陶,然后再丟棄這個(gè)對(duì)象刃泡。大量的對(duì)象就這樣被創(chuàng)建出來(lái),占用大量的內(nèi)存和 jvm空間碉怔。代碼如下:
package com.peidasoft.dateformat;
importjava.text.ParseException;
import java.text.SimpleDateFormat;
importjava.util.Date;
public class DateUtil?{
public static String formatDate(Date date)
throws ParseException{
SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
returnsdf.format(date);
}
public static Date parse(String strDate)
throws ParseException{
SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
returnsdf.parse(strDate);
}
}
你也許會(huì)說(shuō)烘贴,OK,那我就創(chuàng)建一個(gè)靜態(tài)的simpleDateFormat實(shí)例撮胧,然后放到一個(gè)DateUtil類(如下)中桨踪,在使用時(shí)直接使用這個(gè)實(shí)例進(jìn)行操作,這樣問(wèn)題就解決了芹啥。改進(jìn)后的代碼如下:
packagecom.peidasoft.dateformat;
import java.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassDateUtil
?{
private static finalSimpleDateFormat sdf =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static String formatDate(Date date)
throws ParseException{returnsdf.format(date);
}
public static Date parse(String strDate)
throws ParseException{returnsdf.parse(strDate);
}
}
當(dāng)然锻离,這個(gè)方法的確很不錯(cuò)铺峭,在大部分的時(shí)間里面都會(huì)工作得很好。但當(dāng)你在生產(chǎn)環(huán)境中使用一段時(shí)間之后汽纠,你就會(huì)發(fā)現(xiàn)這么一個(gè)事實(shí):它不是線程安全的卫键。在正常的測(cè)試情況之下,都沒(méi)有問(wèn)題虱朵,但一旦在生產(chǎn)環(huán)境中一定負(fù)載情況下時(shí)莉炉,這個(gè)問(wèn)題就出來(lái)了。他會(huì)出現(xiàn)各種不同的情況卧秘,比如轉(zhuǎn)化的時(shí)間不正確呢袱,比如報(bào)錯(cuò),比如線程被掛死等等翅敌。我們看下面的測(cè)試用例羞福,那事實(shí)說(shuō)話:
packagecom.peidasoft.dateformat;
impor java.text.ParseException;
import java.text.SimpleDateFormat;
importjava.util.Date;
public classDateUtil {
private ?static final ?SimpleDateFormat sdf =new ?SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static ?String formatDate(Date date)
throwsParseException{returnsdf.format(date);
}
public ?staticDate parse(String strDate)
throwsParseException{returnsdf.parse(strDate);
}
}
packagecom.peidasoft.dateformat;
import ?java.text.ParseException;
import java.util.Date;
public ?class ?DateUtilTest {
public ?static ?classTestSimpleDateFormatThreadSafe ? extends ?Thread {
@Override
public ?void ?run() {
while(true) {
try{
this.join(2000);
}catch(InterruptedException e1) {
e1.printStackTrace();
}try{
System.out.println(this.getName()+":"+DateUtil.parse("2013-05-24 06:02:20"));
}catch(ParseException e) {
e.printStackTrace();
}
}
}
}public static void main(String[] args) {
for(inti = 0; i < 3; i++)
{
new ?TestSimpleDateFormatThreadSafe().start();
}
}
}
執(zhí)行輸出如下:
Exception in thread "Thread-1"java.lang.NumberFormatException: multiple points
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1082)
at java.lang.Double.parseDouble(Double.java:510)
at java.text.DigitList.getDouble(DigitList.java:151)
at java.text.DecimalFormat.parse(DecimalFormat.java:1302)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
at java.text.DateFormat.parse(DateFormat.java:335)
at com.peidasoft.orm.dateformat.DateNoStaticUtil.parse(DateNoStaticUtil.java:17)
at com.peidasoft.orm.dateformat.DateUtilTest$TestSimpleDateFormatThreadSafe.run(DateUtilTest.java:20)
Exception in thread"Thread-0"java.lang.NumberFormatException: multiple points
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1082)
at java.lang.Double.parseDouble(Double.java:510)
at java.text.DigitList.getDouble(DigitList.java:151)
at java.text.DecimalFormat.parse(DecimalFormat.java:1302)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1589)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1311)
at java.text.DateFormat.parse(DateFormat.java:335)
at com.peidasoft.orm.dateformat.DateNoStaticUtil.parse(DateNoStaticUtil.java:17)
at com.peidasoft.orm.dateformat.DateUtilTest$TestSimpleDateFormatThreadSafe.run(DateUtilTest.java:20)
Thread-2:Mon May 24 06:02:20 CST 2021
Thread-2:Fri May 24 06:02:20 CST 2013
Thread-2:Fri May 24 06:02:20 CST 2013
Thread-2:Fri May 24 06:02:20 CST 2013
說(shuō)明:Thread-1和Thread-0報(bào)java.lang.NumberFormatException: multiple points錯(cuò)誤,直接掛死蚯涮,沒(méi)起來(lái)治专;Thread-2 雖然沒(méi)有掛死,但輸出的時(shí)間是有錯(cuò)誤的遭顶,比如我們輸入的時(shí)間是:2013-05-24 06:02:20 张峰,當(dāng)會(huì)輸出:Mon May 24 06:02:20 CST 2021 這樣的靈異事件。
二.原因
作為一個(gè)專業(yè)程序員棒旗,我們當(dāng)然都知道喘批,相比于共享一個(gè)變量的開(kāi)銷要比每次創(chuàng)建一個(gè)新變量要小很多。上面的優(yōu)化過(guò)的靜態(tài)的SimpleDateFormat版铣揉,之所在并發(fā)情況下回出現(xiàn)各種靈異錯(cuò)誤饶深,是因?yàn)镾impleDateFormat和DateFormat類不是線程安全的。我們之所以忽視線程安全的問(wèn)題逛拱,是因?yàn)閺腟impleDateFormat和DateFormat類提供給我們的接口上來(lái)看敌厘,實(shí)在讓人看不出它與線程安全有何相干。只是在JDK文檔的最下面有如下說(shuō)明:
SimpleDateFormat中的日期格式不是同步的朽合。推薦(建議)為每個(gè)線程創(chuàng)建獨(dú)立的格式實(shí)例俱两。如果多個(gè)線程同時(shí)訪問(wèn)一個(gè)格式,則它必須保持外部同步曹步。
JDK原始文檔如下:
Synchronization:
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.
下面我們通過(guò)看JDK源碼來(lái)看看為什么SimpleDateFormat和DateFormat類不是線程安全的真正原因:
SimpleDateFormat繼承了DateFormat,在DateFormat中定義了一個(gè)protected屬性的 Calendar類的對(duì)象:calendar宪彩。只是因?yàn)镃alendar累的概念復(fù)雜,牽扯到時(shí)區(qū)與本地化等等讲婚,Jdk的實(shí)現(xiàn)中使用了成員變量來(lái)傳遞參數(shù)毯焕,這就造成在多線程的時(shí)候會(huì)出現(xiàn)錯(cuò)誤。
在format方法里磺樱,有這樣一段代碼:
private ?StringBuffer format(Date date, StringBuffer toAppendTo,
Field Delegate delegate)
?{//Convert input date to time field listcalendar.setTime(date);
booleanuseDateFormatSymbols =useDateFormatSymbols();
for(inti = 0; i >> 8;intcount = compiledPattern[i++] & 0xff;
if(count == 255) {
count= compiledPattern[i++] << 16;
count|= compiledPattern[i++];
}
switch(tag) {
caseTAG_QUOTE_ASCII_CHAR:
toAppendTo.append((char)count);break;
caseTAG_QUOTE_CHARS:
toAppendTo.append(compiledPattern, i, count);
i+=count;break;default:
subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols);
break;
}
}returntoAppendTo;
}
calendar.setTime(date)這條語(yǔ)句改變了calendar纳猫,稍后,calendar還會(huì)用到(在subFormat方法里)竹捉,而這就是引發(fā)問(wèn)題的根源芜辕。想象一下,在一個(gè)多線程環(huán)境下块差,有兩個(gè)線程持有了同一個(gè)SimpleDateFormat的實(shí)例侵续,分別調(diào)用format方法:
線程1調(diào)用format方法,改變了calendar這個(gè)字段憨闰。
中斷來(lái)了状蜗。
線程2開(kāi)始執(zhí)行,它也改變了calendar鹉动。
又中斷了轧坎。
線程1回來(lái)了,此時(shí)泽示,calendar已然不是它所設(shè)的值缸血,而是走上了線程2設(shè)計(jì)的道路。如果多個(gè)線程同時(shí)爭(zhēng)搶calendar對(duì)象械筛,則會(huì)出現(xiàn)各種問(wèn)題捎泻,時(shí)間不對(duì),線程掛死等等埋哟。
分析一下format的實(shí)現(xiàn)笆豁,我們不難發(fā)現(xiàn),用到成員變量calendar赤赊,唯一的好處闯狱,就是在調(diào)用subFormat時(shí),少了一個(gè)參數(shù)砍鸠,卻帶來(lái)了這許多的問(wèn)題扩氢。其實(shí),只要在這里用一個(gè)局部變量爷辱,一路傳遞下去录豺,所有問(wèn)題都將迎刃而解。
這個(gè)問(wèn)題背后隱藏著一個(gè)更為重要的問(wèn)題--無(wú)狀態(tài):無(wú)狀態(tài)方法的好處之一饭弓,就是它在各種環(huán)境下双饥,都可以安全的調(diào)用。衡量一個(gè)方法是否是有狀態(tài)的弟断,就看它是否改動(dòng)了其它的東西咏花,比如全局變量,比如實(shí)例的字段。format方法在運(yùn)行過(guò)程中改動(dòng)了SimpleDateFormat的calendar字段昏翰,所以苍匆,它是有狀態(tài)的。
這也同時(shí)提醒我們?cè)陂_(kāi)發(fā)和設(shè)計(jì)系統(tǒng)的時(shí)候注意下一下三點(diǎn):
1.自己寫(xiě)公用類的時(shí)候棚菊,要對(duì)多線程調(diào)用情況下的后果在注釋里進(jìn)行明確說(shuō)明
2.對(duì)線程環(huán)境下浸踩,對(duì)每一個(gè)共享的可變變量都要注意其線程安全性
3.我們的類和方法在做設(shè)計(jì)的時(shí)候,要盡量設(shè)計(jì)成無(wú)狀態(tài)的
三.解決辦法
1.需要的時(shí)候創(chuàng)建新實(shí)例:
package ? com.peidasoft.dateformat;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
public ? class ? DateUtil{
public ?static ?String formatDate(Date date)throwsParseException{
SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
returnsdf.format(date);
}public
staticDate parse(String strDate)throwsParseException{
SimpleDateFormat sdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
returnsdf.parse(strDate);
}
}
說(shuō)明:在需要用到SimpleDateFormat的地方新建一個(gè)實(shí)例统求,不管什么時(shí)候检碗,將有線程安全問(wèn)題的對(duì)象由共享變?yōu)榫植克接卸寄鼙苊舛嗑€程問(wèn)題,不過(guò)也加重了創(chuàng)建對(duì)象的負(fù)擔(dān)码邻。在一般情況下折剃,這樣其實(shí)對(duì)性能影響比不是很明顯的。
2.使用同步:同步SimpleDateFormat對(duì)象
package ? com.peidasoft.dateformat;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
public ?class ? Date SyncUtil {
private ?static ?SimpleDateFormat sdf =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
publicstaticString formatDate(Date date)throwsParseException{
synchronized(sdf){
returnsdf.format(date);
}
}
public ?staticDate parse(String strDate)throwsParseException{
synchronized(sdf){returnsdf.parse(strDate);
}
}
}
說(shuō)明:當(dāng)線程較多時(shí)像屋,當(dāng)一個(gè)線程調(diào)用該方法時(shí)怕犁,其他想要調(diào)用此方法的線程就要block,多線程并發(fā)量大的時(shí)候會(huì)對(duì)性能有一定的影響开睡。
3.使用ThreadLocal:
package ??com.peidasoft.dateformat;
importjava.text.DateFormat;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
public ?class ?ConcurrentDateUtil {
privatestaticThreadLocal threadLocal =newThreadLocal() {
@Override
protectedDateFormat initialValue() {
return ?newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
public staticDate parse(String dateStr)throwsParseException {
returnthreadLocal.get().parse(dateStr);
}
publicstaticString format(Date date) {
returnthreadLocal.get().format(date);
}
}
另外一種寫(xiě)法:
package ? com.peidasoft.dateformat;
importjava.text.DateFormat;
importjava.text.ParseException;
importjava.text.SimpleDateFormat;
importjava.util.Date;
public ?class ?ThreadLocalDateUtil {
private ?static ?finalString date_format = "yyyy-MM-dd HH:mm:ss";
private ?static ?ThreadLocal threadLocal =newThreadLocal();
public ?static ?DateFormat getDateFormat()
{
DateFormat df=threadLocal.get();if(df==null){
df=newSimpleDateFormat(date_format);
threadLocal.set(df);
}returndf;
}public ?static ?String formatDate(Date date)throwsParseException {
returngetDateFormat().format(date);
}
public ?static ? Date parse(String strDate)throwsParseException?
returngetDateFormat().parse(strDate);
}
}
說(shuō)明:使用ThreadLocal, 也是將共享變量變?yōu)楠?dú)享因苹,線程獨(dú)享肯定能比方法獨(dú)享在并發(fā)環(huán)境中能減少不少創(chuàng)建對(duì)象的開(kāi)銷。如果對(duì)性能要求比較高的情況下篇恒,一般推薦使用這種方法扶檐。
4.拋棄JDK,使用其他類庫(kù)中的時(shí)間格式化類:
1.使用Apache commons 里的FastDateFormat胁艰,宣稱是既快又線程安全的SimpleDateFormat, 可惜它只能對(duì)日期進(jìn)行format, 不能對(duì)日期串進(jìn)行解析款筑。
2.使用Joda-Time類庫(kù)來(lái)處理時(shí)間相關(guān)問(wèn)題
做一個(gè)簡(jiǎn)單的壓力測(cè)試,方法一最慢腾么,方法三最快奈梳,但是就算是最慢的方法一性能也不差,一般系統(tǒng)方法一和方法二就可以滿足解虱,所以說(shuō)在這個(gè)點(diǎn)很難成為你系統(tǒng)的瓶頸所在攘须。從簡(jiǎn)單的角度來(lái)說(shuō),建議使用方法一或者方法二殴泰,如果在必要的時(shí)候于宙,追求那么一點(diǎn)性能提升的話,可以考慮用方法三悍汛,用ThreadLocal做緩存捞魁。
Joda-Time類庫(kù)對(duì)時(shí)間處理方式比較完美,建議使用离咐。
參考資料:
1.http://dreamhead.blogbus.com/logs/215637834.html
2.http://www.blogjava.net/killme2008/archive/2011/07/10/354062.html