亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java?SimpleDateFormat線(xiàn)程不安全問(wèn)題

 更新時(shí)間:2023年03月24日 15:32:00   作者:堅(jiān)持與努力  
這篇文章詳細(xì)介紹了如可解決impleDateFormat線(xiàn)程不安全的問(wèn)題,對(duì)多線(xiàn)程問(wèn)題感興趣的同學(xué)可以參考閱讀本文

多線(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

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

    基于java中兩個(gè)對(duì)象屬性的比較

    下面小編就為大家?guī)?lái)一篇基于java中兩個(gè)對(duì)象屬性的比較。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • MyBatis insert語(yǔ)句返回主鍵和selectKey標(biāo)簽方式

    MyBatis insert語(yǔ)句返回主鍵和selectKey標(biāo)簽方式

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

    Spring 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ō)明

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

    Java中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-05
  • springboot的war和jar包的使用詳解

    springboot的war和jar包的使用詳解

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

    Springboot如何實(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)(包含格式和圖片)

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

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

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

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

最新評(píng)論