欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java?SimpleDateFormat線程不安全問題

 更新時間:2023年03月24日 15:32:00   作者:堅持與努力  
這篇文章詳細介紹了如可解決impleDateFormat線程不安全的問題,對多線程問題感興趣的同學可以參考閱讀本文

多線程 ——SimpleDateFormat

public class DateTest {
    //工具類中的日期組件
    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ā)情況下,存在安全性問題。

我們通過源碼看下:

SimpleDateFormat繼承了 DateFormat

DateFormat類中維護了一個全局的Calendar變量

sdf.parse(dateStr)和sdf.format(date),都是由Calendar引用來儲存的。

如果SimpleDateFormat是static全局共享的,Calendar引用也會被共享。

又因為Calendar內(nèi)部并沒有線程安全機制,所以全局共享的SimpleDateFormat不是線性安全的。

解決方法

解決方法1

「FastDateFormat(FastDateFormat能保證線程安全) 替換 SimpleDateFormat」

private static final FastDateFormat sdf = FastDateFormat.getInstance(“yyyy-MM-dd HH:mm:ss”);

 測試代碼如下所示:

public class DateTest {
    //工具類中的日期組件
//    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是線程安全的,java 8+支持)代替SimpleDateFormat」

private static DateTimeFormatter sdf = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm:ss”);

測試代碼如下:

public class DateTest {
//工具類中的日期組件
// 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é)

在多線程中使用全局變量時一定要考慮到線程安全問題,若不確定是否存在線程安全問題的公共變量,則不要冒然使用,可以做一些測試和資料分析,或者使用局部變量。

到此這篇關于Java SimpleDateFormat線程不安全問題的文章就介紹到這了,更多相關SimpleDateFormat線程不安全內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 淺談Java包裝類型Long的==操作引發(fā)的低級bug

    淺談Java包裝類型Long的==操作引發(fā)的低級bug

    本文主要介紹了淺談Java包裝類型Long的==操作引發(fā)的低級bug,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 基于java中兩個對象屬性的比較

    基于java中兩個對象屬性的比較

    下面小編就為大家?guī)硪黄趈ava中兩個對象屬性的比較。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • MyBatis insert語句返回主鍵和selectKey標簽方式

    MyBatis insert語句返回主鍵和selectKey標簽方式

    這篇文章主要介紹了MyBatis insert語句返回主鍵和selectKey標簽方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring Boot和Thymeleaf整合結(jié)合JPA實現(xiàn)分頁效果(實例代碼)

    Spring Boot和Thymeleaf整合結(jié)合JPA實現(xiàn)分頁效果(實例代碼)

    這篇文章主要介紹了Spring Boot和Thymeleaf整合結(jié)合JPA實現(xiàn)分頁效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • 關于Mybatis與JPA的優(yōu)缺點說明

    關于Mybatis與JPA的優(yōu)缺點說明

    這篇文章主要介紹了關于Mybatis與JPA的優(yōu)缺點說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java中Controller、Service、Dao/Mapper層的區(qū)別與用法

    Java中Controller、Service、Dao/Mapper層的區(qū)別與用法

    在Java開發(fā)中,通常會采用三層架構(gòu)(或稱MVC架構(gòu))來劃分程序的職責和功能,分別是Controller層、Service層、Dao/Mapper層,本文將詳細給大家介紹了三層的區(qū)別和用法,需要的朋友可以參考下
    2023-05-05
  • springboot的war和jar包的使用詳解

    springboot的war和jar包的使用詳解

    這篇文章主要介紹了springboot的war和jar包的使用詳解,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2018-11-11
  • Springboot如何實現(xiàn)自定義異常數(shù)據(jù)

    Springboot如何實現(xiàn)自定義異常數(shù)據(jù)

    這篇文章主要介紹了Springboot如何實現(xiàn)自定義異常數(shù)據(jù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • 如何使用Java將word解析出來(包含格式和圖片)

    如何使用Java將word解析出來(包含格式和圖片)

    今天遇到一個讀取word模板內(nèi)容的需求,下面這篇文章主要給大家介紹了關于如何使用Java將word解析出來,包含格式和圖片,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • Java非侵入式API接口文檔工具apigcc用法詳解

    Java非侵入式API接口文檔工具apigcc用法詳解

    這篇文章主要介紹了Java非侵入式API接口文檔工具apigcc用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07

最新評論