SimpleDateFormat線程安全問題排查詳解
一. 問題現(xiàn)象
運營部門反饋使用小程序配置的拉新現(xiàn)金紅包活動二維碼,在掃碼后跳轉(zhuǎn)至404頁面。
二. 原因排查
首先,檢查掃碼后的跳轉(zhuǎn)鏈接地址不是對應(yīng)二維碼的實際URL,根據(jù)代碼邏輯推測,可能是accessToken在微信端已失效導(dǎo)致,檢查數(shù)據(jù)發(fā)現(xiàn),數(shù)據(jù)庫存儲的accessToken過期時間為2022-11-29(排查問題當日為2022-10-08),發(fā)現(xiàn)過期時間太長,導(dǎo)致accessToken未刷新導(dǎo)致。
接下來,繼續(xù)排查造成這一問題的真正原因。排查日志發(fā)現(xiàn)更新sql語句對應(yīng)的的過期時間與數(shù)據(jù)庫記錄的一致,推測賦值代碼存在問題,如下。
tokenInfo.setExpireTime(simpleDateFormat.parse(token.getString("expireTime")));
其中,simpleDateFormat在代碼中定義是該類的成員變量。
- 跟蹤代碼后發(fā)現(xiàn)源碼中有明確說明SimpleDateFormat不應(yīng)該應(yīng)用于多線程場景下。
Synchronization //SimpleDateFormat中的日期格式化不是同步的。 Date formats are not synchronized. //建議為每個線程創(chuàng)建獨立的格式實例。 It is recommended to create separate format instances for each thread. //如果多個線程同時訪問一個格式,則它必須保持外部同步。 If multiple threads access a format concurrently, it must be synchronized externally.
- 至此,基本可以判斷是simpleDateFormat.parse在多線程情況下造成錯誤的過期時間入庫,導(dǎo)致accesstoken無法正常更新。
三. 原因分析
- 接下來寫個測試類來模擬:
@RunWith(SpringRunner.class) @SpringBootTest public class SimpleDateFormatTest { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * 定義線程池 **/ private static final ExecutorService threadPool = new ThreadPoolExecutor(16, 20, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(1024), new ThreadFactoryBuilder().setNamePrefix("[線程]").build(), new ThreadPoolExecutor.AbortPolicy() ); @SneakyThrows @Test public void testParse() { Set<String> results = Collections.synchronizedSet(new HashSet<>()); // 每個線程都對相同字符串執(zhí)行“parse日期字符串”的操作,當THREAD_NUMBERS個線程執(zhí)行完畢后,應(yīng)該有且僅有一個相同的結(jié)果才是正確的 String initialDateStr = "2022-10-08 18:30:01"; for (int i = 0; i < 20; i++) { threadPool.execute(() -> { Date parse = null; try { parse = simpleDateFormat.parse(initialDateStr); } catch (ParseException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "---" + parse); }); } threadPool.shutdown(); threadPool.awaitTermination(1, TimeUnit.HOURS); } }
運行結(jié)果如下:
[線程]5---Sat Jan 08 18:30:01 CST 2000
[線程]0---Wed Oct 08 18:30:01 CST 2200
[線程]4---Sat Oct 08 18:30:01 CST 2022
Exception in thread "[線程]3" java.lang.NumberFormatException: multiple points
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.text.DigitList.getDouble(DigitList.java:169)
at java.text.DecimalFormat.parse(DecimalFormat.java:2089)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
at java.text.DateFormat.parse(DateFormat.java:364)
at com.SimpleDateFormatTest.lambda$testParse$0(SimpleDateFormatTest.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
[線程]6---Sat Oct 08 18:30:01 CST 2022
[線程]11---Wed Mar 15 18:30:01 CST 2045
Exception in thread "[線程]2" java.lang.ArrayIndexOutOfBoundsException: 275
at sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:453)
at java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2397)
at java.util.GregorianCalendar.computeTime(GregorianCalendar.java:2818)
at java.util.Calendar.updateTime(Calendar.java:3393)
at java.util.Calendar.getTimeInMillis(Calendar.java:1782)
at java.util.Calendar.getTime(Calendar.java:1755)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1532)
at java.text.DateFormat.parse(DateFormat.java:364)
at com.SimpleDateFormatTest.lambda$testParse$0(SimpleDateFormatTest.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
[線程]6---Fri Oct 01 18:30:01 CST 8202
[線程]12---Sat Oct 08 18:30:01 CST 2022
Exception in thread "[線程]1" java.lang.NumberFormatException: multiple points
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1890)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at java.text.DigitList.getDouble(DigitList.java:169)
at java.text.DecimalFormat.parse(DecimalFormat.java:2089)
at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869)
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514)
at java.text.DateFormat.parse(DateFormat.java:364)
at com.SimpleDateFormatTest.lambda$testParse$0(SimpleDateFormatTest.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
[線程]0---Sat Oct 08 18:30:01 CST 2022
[線程]12---Sat Oct 08 18:30:01 CST 2022
[線程]13---Sat Oct 08 18:30:01 CST 2022
[線程]18---Sat Oct 08 18:30:01 CST 2022
[線程]6---Sat Oct 01 18:30:01 CST 2022
[線程]7---Sat Oct 08 18:30:01 CST 2022
[線程]10---Sat Oct 08 18:30:01 CST 2022
[線程]15---Sat Oct 08 18:00:01 CST 2022
[線程]17---Sat Oct 08 18:30:01 CST 2022
[線程]14---Sat Oct 08 18:30:01 CST 2022
預(yù)期結(jié)果個數(shù) 1---實際結(jié)果個數(shù)7
不僅有的線程結(jié)果不正確,甚至還有一些線程還出現(xiàn)了異常!
- 為什么SimpleDateFormat類不是線程安全的?
SimpleDateFormat繼承了DateFormat,DateFormat內(nèi)部有一個Calendar對象的引用,主要用來存儲和SimpleDateFormat相關(guān)的日期信息。
SimpleDateFormat對parse()方法的實現(xiàn)。關(guān)鍵代碼如下:
@Override public Date parse(String text, ParsePosition pos) { ...省略中間代碼 Date parsedDate; try { ... parsedDate = calb.establish(calendar).getTime(); } catch (IllegalArgumentException e) { ... } return parsedDate; }
establish()的實現(xiàn)如下:
Calendar establish(Calendar cal) { ...省略中間代碼 cal.clear(); for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) { for (int index = 0; index <= maxFieldIndex; index++) { if (field[index] == stamp) { cal.set(index, field[MAX_FIELD + index]); break; } } } ... return cal; }
在多個線程共享SimpleDateFormat時,同時也共享了Calendar引用,在如上代碼中,calendar首先會進行clear()操作,然后進行set操作,在多線程情況下,set操作會覆蓋之前的值,而且在后續(xù)對日期進行操作時,也可能會因為clear操作被清除導(dǎo)致異常。
四. 解決方案
- 將SimpleDateFormat定義成局部變量,每次使用時都new一個新對象,頻繁創(chuàng)建對象消耗大,性能影響一些(JDK文檔推薦此做法)
public static Date parse(String strDate) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.parse(strDate); }
- 維護一個SimpleDateFormat實體,轉(zhuǎn)換方法上使用 Synchronized 保證線程安全:多線程堵塞(并發(fā)大系統(tǒng)不推薦)
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static String formatDate(Date date)throws ParseException{ synchronized(sdf){ return sdf.format(date); } } public static Date parse(String strDate) throws ParseException{ synchronized(sdf){ return sdf.parse(strDate); } }
- 使用ThreadLocal : 線程獨享不堵塞,并且減少創(chuàng)建對象的開銷(如果對性能要求比較高的情況,推薦這種方式)。
public static ThreadLocal<DateFormat> threadLocal = ThreadLocal.withInitial( () -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ); public static Date parse(String strDate) throws ParseException { return threadLocal.get().parse(strDate); }
- DateTimeFormatter是Java8提供的新的日期時間API中的類,DateTimeFormatter類是線程安全的,可以在高并發(fā)場景下直接使用。
String dateTimeStr= "2016-10-25 12:00:00"; DateTimeFormatter formatter02 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime localDateTime = LocalDateTime.parse(dateTimeStr,formatter02); System.out.println(localDateTime); String format = localDateTime.format(formatter02); System.out.println(format); 2016-10-25T12:00 2016-10-25 12:00:00
最終,我們根據(jù)實際情況公共包DateUtil類提供的strConvertDate方法,原理是按照方案1來解決該問題。
以上就是SimpleDateFormat線程安全問題排查詳解的詳細內(nèi)容,更多關(guān)于SimpleDateFormat線程安全排查的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
openEuler?搭建java開發(fā)環(huán)境的詳細過程
這篇文章主要介紹了openEuler?搭建java開發(fā)環(huán)境,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例
這篇文章主要介紹了Spring Cloud Alibaba 使用 Feign+Sentinel 完成熔斷的示例,幫助大家更好的理解和學(xué)習(xí)使用Spring Cloud,感興趣的朋友可以了解下2021-03-03springboot?vue前后端接口測試樹結(jié)點添加功能
這篇文章主要為大家介紹了springboot?vue前后端接口測試樹結(jié)點添加功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實例
這篇文章主要介紹了java通過ssh連接服務(wù)器執(zhí)行shell命令詳解及實例方法的相關(guān)資料2017-02-02