Java?SimpleDateFormat線(xiàn)程不安全問(wèn)題
多線(xiàn)程 ——SimpleDateFormat
public class DateTest { //工具類(lèi)中的日期組件 private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws Exception { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(10)); for (int i = 0; i < 100; i++) { threadPoolExecutor.execute(() -> { String dateString = sdf.format(new Date()); try { Date parseDate = sdf.parse(dateString); String dateString2 = sdf.format(parseDate); System.out.println(dateString.equals(dateString2)); } catch (Exception e) { e.printStackTrace(); } }); } } }
結(jié)果
原因分析
全局變量的SimpleDateFormat,在并發(fā)情況下,存在安全性問(wèn)題。
我們通過(guò)源碼看下:
SimpleDateFormat繼承了 DateFormat
DateFormat類(lèi)中維護(hù)了一個(gè)全局的Calendar變量
sdf.parse(dateStr)和sdf.format(date),都是由Calendar引用來(lái)儲(chǔ)存的。
如果SimpleDateFormat是static全局共享的,Calendar引用也會(huì)被共享。
又因?yàn)镃alendar內(nèi)部并沒(méi)有線(xiàn)程安全機(jī)制,所以全局共享的SimpleDateFormat不是線(xiàn)性安全的。
解決方法
解決方法1
「FastDateFormat(FastDateFormat能保證線(xiàn)程安全) 替換 SimpleDateFormat」
private static final FastDateFormat sdf = FastDateFormat.getInstance(“yyyy-MM-dd HH:mm:ss”);
測(cè)試代碼如下所示:
public class DateTest { //工具類(lèi)中的日期組件 // private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final FastDateFormat sdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws Exception { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(10)); for (int i = 0; i < 100; i++) { threadPoolExecutor.execute(() -> { String dateString = sdf.format(new Date()); try { Date parseDate = sdf.parse(dateString); String dateString2 = sdf.format(parseDate); System.out.println(dateString.equals(dateString2)); } catch (Exception e) { e.printStackTrace(); } }); } threadPoolExecutor.shutdown(); } }
打印結(jié)果:
解決方法2
「使用DateTimeFormatter(DateTimeFormatter是線(xiàn)程安全的,java 8+支持)代替SimpleDateFormat」
private static DateTimeFormatter sdf = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
測(cè)試代碼如下:
public class DateTest {
//工具類(lèi)中的日期組件
// private static final SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
private static DateTimeFormatter sdf = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);
// private static final FastDateFormat sdf = FastDateFormat.getInstance(“yyyy-MM-dd HH:mm:ss”);
public static void main(String[] args) throws Exception { ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 100, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(10)); for (int i = 0; i < 100; i++) { threadPoolExecutor.execute(() -> { try { String dateString = sdf.format(LocalDateTime.now()); TemporalAccessor temporalAccessor = sdf.parse(dateString); String dateString2 = sdf.format(temporalAccessor); System.out.println(dateString.equals(dateString2)); } catch (Exception e) { e.printStackTrace(); } }); } threadPoolExecutor.shutdown(); }
}
打印結(jié)果如下:
總結(jié)
在多線(xiàn)程中使用全局變量時(shí)一定要考慮到線(xiàn)程安全問(wèn)題,若不確定是否存在線(xiàn)程安全問(wèn)題的公共變量,則不要冒然使用,可以做一些測(cè)試和資料分析,或者使用局部變量。
到此這篇關(guān)于Java SimpleDateFormat線(xiàn)程不安全問(wèn)題的文章就介紹到這了,更多相關(guān)SimpleDateFormat線(xiàn)程不安全內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Java包裝類(lèi)型Long的==操作引發(fā)的低級(jí)bug
本文主要介紹了淺談Java包裝類(lèi)型Long的==操作引發(fā)的低級(jí)bug,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08MyBatis insert語(yǔ)句返回主鍵和selectKey標(biāo)簽方式
這篇文章主要介紹了MyBatis insert語(yǔ)句返回主鍵和selectKey標(biāo)簽方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)
這篇文章主要介紹了Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02關(guān)于Mybatis與JPA的優(yōu)缺點(diǎn)說(shuō)明
這篇文章主要介紹了關(guān)于Mybatis與JPA的優(yōu)缺點(diǎn)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Java中Controller、Service、Dao/Mapper層的區(qū)別與用法
在Java開(kāi)發(fā)中,通常會(huì)采用三層架構(gòu)(或稱(chēng)MVC架構(gòu))來(lái)劃分程序的職責(zé)和功能,分別是Controller層、Service層、Dao/Mapper層,本文將詳細(xì)給大家介紹了三層的區(qū)別和用法,需要的朋友可以參考下2023-05-05Springboot如何實(shí)現(xiàn)自定義異常數(shù)據(jù)
這篇文章主要介紹了Springboot如何實(shí)現(xiàn)自定義異常數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09如何使用Java將word解析出來(lái)(包含格式和圖片)
今天遇到一個(gè)讀取word模板內(nèi)容的需求,下面這篇文章主要給大家介紹了關(guān)于如何使用Java將word解析出來(lái),包含格式和圖片,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12