【高并發(fā)SimpleDateFormat類的線程安全問題和解決方案(附6種解決方案)

      網(wǎng)友投稿 623 2025-04-04

      大家好,我是冰河~~

      首先問下大家:你使用的SimpleDateFormat類還安全嗎?為什么說SimpleDateFormat類不是線程安全的?帶著問題從本文中尋求答案。

      提起SimpleDateFormat類,想必做過Java開發(fā)的童鞋都不會感到陌生。沒錯,它就是Java中提供的日期時間的轉(zhuǎn)化類。這里,為什么說SimpleDateFormat類有線程安全問題呢?有些小伙伴可能會提出疑問:我們生產(chǎn)環(huán)境上一直在使用SimpleDateFormat類來解析和格式化日期和時間類型的數(shù)據(jù),一直都沒有問題啊!我的回答是:沒錯,那是因為你們的系統(tǒng)達(dá)不到SimpleDateFormat類出現(xiàn)問題的并發(fā)量,也就是說你們的系統(tǒng)沒啥負(fù)載!

      接下來,我們就一起看下在高并發(fā)下SimpleDateFormat類為何會出現(xiàn)安全問題,以及如何解決SimpleDateFormat類的安全問題。

      重現(xiàn)SimpleDateFormat類的線程安全問題

      為了重現(xiàn)SimpleDateFormat類的線程安全問題,一種比較簡單的方式就是使用線程池結(jié)合Java并發(fā)包中的CountDownLatch類和Semaphore類來重現(xiàn)線程安全問題。

      有關(guān)CountDownLatch類和Semaphore類的具體用法和底層原理與源碼解析在【高并發(fā)專題】后文會深度分析。這里,大家只需要知道CountDownLatch類可以使一個線程等待其他線程各自執(zhí)行完畢后再執(zhí)行。而Semaphore類可以理解為一個計數(shù)信號量,必須由獲取它的線程釋放,經(jīng)常用來限制訪問某些資源的線程數(shù)量,例如限流等。

      好了,先來看下重現(xiàn)SimpleDateFormat類的線程安全問題的代碼,如下所示。

      package io.binghe.concurrent.lab06; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author binghe * @version 1.0.0 * @description 測試SimpleDateFormat的線程不安全問題 */ public class SimpleDateFormatTest01 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; //SimpleDateFormat對象 private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { simpleDateFormat.parse("2020-01-01"); } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號量發(fā)生錯誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }

      可以看到,在SimpleDateFormatTest01類中,首先定義了兩個常量,一個是程序執(zhí)行的總次數(shù),一個是同時運(yùn)行的線程數(shù)量。程序中結(jié)合線程池和CountDownLatch類與Semaphore類來模擬高并發(fā)的業(yè)務(wù)場景。其中,有關(guān)日期轉(zhuǎn)化的代碼只有如下一行。

      simpleDateFormat.parse("2020-01-01");

      當(dāng)程序捕獲到異常時,打印相關(guān)的信息,并退出整個程序的運(yùn)行。當(dāng)程序正確運(yùn)行后,會打印“所有線程格式化日期成功”。

      運(yùn)行程序輸出的結(jié)果信息如下所示。

      Exception in thread "pool-1-thread-4" Exception in thread "pool-1-thread-1" Exception in thread "pool-1-thread-2" 線程:pool-1-thread-7 格式化日期失敗 線程:pool-1-thread-9 格式化日期失敗 線程:pool-1-thread-10 格式化日期失敗 Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-5" Exception in thread "pool-1-thread-6" 線程:pool-1-thread-15 格式化日期失敗 線程:pool-1-thread-21 格式化日期失敗 Exception in thread "pool-1-thread-23" 線程:pool-1-thread-16 格式化日期失敗 線程:pool-1-thread-11 格式化日期失敗 java.lang.ArrayIndexOutOfBoundsException 線程:pool-1-thread-27 格式化日期失敗 at java.lang.System.arraycopy(Native Method) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:597) at java.lang.StringBuffer.append(StringBuffer.java:367) at java.text.DigitList.getLong(DigitList.java:191)線程:pool-1-thread-25 格式化日期失敗 at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) 線程:pool-1-thread-14 格式化日期失敗 at java.text.DateFormat.parse(DateFormat.java:364) at io.binghe.concurrent.lab06.SimpleDateFormatTest01.lambda$main

      Exception in thread "pool-1-thread-4" Exception in thread "pool-1-thread-1" Exception in thread "pool-1-thread-2" 線程:pool-1-thread-7 格式化日期失敗 線程:pool-1-thread-9 格式化日期失敗 線程:pool-1-thread-10 格式化日期失敗 Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-5" Exception in thread "pool-1-thread-6" 線程:pool-1-thread-15 格式化日期失敗 線程:pool-1-thread-21 格式化日期失敗 Exception in thread "pool-1-thread-23" 線程:pool-1-thread-16 格式化日期失敗 線程:pool-1-thread-11 格式化日期失敗 java.lang.ArrayIndexOutOfBoundsException 線程:pool-1-thread-27 格式化日期失敗 at java.lang.System.arraycopy(Native Method) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:597) at java.lang.StringBuffer.append(StringBuffer.java:367) at java.text.DigitList.getLong(DigitList.java:191)線程:pool-1-thread-25 格式化日期失敗 at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) 線程:pool-1-thread-14 格式化日期失敗 at java.text.DateFormat.parse(DateFormat.java:364) at io.binghe.concurrent.lab06.SimpleDateFormatTest01.lambda$main$0(SimpleDateFormatTest01.java:47) 線程:pool-1-thread-13 格式化日期失敗 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) java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 線程:pool-1-thread-20 格式化日期失敗 at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at io.binghe.concurrent.lab06.SimpleDateFormatTest01.lambda$main$0(SimpleDateFormatTest01.java:47) 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) java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) Process finished with exit code 1

      (SimpleDateFormatTest01.java:47) 線程:pool-1-thread-13 格式化日期失敗 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) java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 線程:pool-1-thread-20 格式化日期失敗 at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at io.binghe.concurrent.lab06.SimpleDateFormatTest01.lambda$main

      Exception in thread "pool-1-thread-4" Exception in thread "pool-1-thread-1" Exception in thread "pool-1-thread-2" 線程:pool-1-thread-7 格式化日期失敗 線程:pool-1-thread-9 格式化日期失敗 線程:pool-1-thread-10 格式化日期失敗 Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-5" Exception in thread "pool-1-thread-6" 線程:pool-1-thread-15 格式化日期失敗 線程:pool-1-thread-21 格式化日期失敗 Exception in thread "pool-1-thread-23" 線程:pool-1-thread-16 格式化日期失敗 線程:pool-1-thread-11 格式化日期失敗 java.lang.ArrayIndexOutOfBoundsException 線程:pool-1-thread-27 格式化日期失敗 at java.lang.System.arraycopy(Native Method) at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:597) at java.lang.StringBuffer.append(StringBuffer.java:367) at java.text.DigitList.getLong(DigitList.java:191)線程:pool-1-thread-25 格式化日期失敗 at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) 線程:pool-1-thread-14 格式化日期失敗 at java.text.DateFormat.parse(DateFormat.java:364) at io.binghe.concurrent.lab06.SimpleDateFormatTest01.lambda$main$0(SimpleDateFormatTest01.java:47) 線程:pool-1-thread-13 格式化日期失敗 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) java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 線程:pool-1-thread-20 格式化日期失敗 at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2162) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) at io.binghe.concurrent.lab06.SimpleDateFormatTest01.lambda$main$0(SimpleDateFormatTest01.java:47) 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) java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) Process finished with exit code 1

      (SimpleDateFormatTest01.java:47) 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) java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:601) at java.lang.Long.parseLong(Long.java:631) at java.text.DigitList.getLong(DigitList.java:195) at java.text.DecimalFormat.parse(DecimalFormat.java:2084) at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:1869) at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1514) at java.text.DateFormat.parse(DateFormat.java:364) Process finished with exit code 1

      說明,在高并發(fā)下使用SimpleDateFormat類格式化日期時拋出了異常,SimpleDateFormat類不是線程安全的!!!

      接下來,我們就看下,SimpleDateFormat類為何不是線程安全的。

      SimpleDateFormat類為何不是線程安全的?

      那么,接下來,我們就一起來看看真正引起SimpleDateFormat類線程不安全的根本原因。

      通過查看SimpleDateFormat類的源碼,我們得知:SimpleDateFormat是繼承自DateFormat類,DateFormat類中維護(hù)了一個全局的Calendar變量,如下所示。

      /** * The {@link Calendar} instance used for calculating the date-time fields * and the instant of time. This field is used for both formatting and * parsing. * *

      Subclasses should initialize this field to a {@link Calendar} * appropriate for the {@link Locale} associated with this * DateFormat. * @serial */ protected Calendar calendar;

      從注釋可以看出,這個Calendar對象既用于格式化也用于解析日期時間。接下來,我們再查看parse()方法接近最后的部分。

      @Override public Date parse(String text, ParsePosition pos){ ################此處省略N行代碼################## Date parsedDate; try { parsedDate = calb.establish(calendar).getTime(); // If the year value is ambiguous, // then the two-digit year == the default start year if (ambiguousYear[0]) { if (parsedDate.before(defaultCenturyStart)) { parsedDate = calb.addYear(100).establish(calendar).getTime(); } } } // An IllegalArgumentException will be thrown by Calendar.getTime() // if any fields are out of range, e.g., MONTH == 17. catch (IllegalArgumentException e) { pos.errorIndex = start; pos.index = oldStart; return null; } return parsedDate; }

      可見,最后的返回值是通過調(diào)用CalendarBuilder.establish()方法獲得的,而這個方法的參數(shù)正好就是前面的Calendar對象。

      接下來,我們再來看看CalendarBuilder.establish()方法,如下所示。

      Calendar establish(Calendar cal) { boolean weekDate = isSet(WEEK_YEAR) && field[WEEK_YEAR] > field[YEAR]; if (weekDate && !cal.isWeekDateSupported()) { // Use YEAR instead if (!isSet(YEAR)) { set(YEAR, field[MAX_FIELD + WEEK_YEAR]); } weekDate = false; } cal.clear(); // Set the fields from the min stamp to the max stamp so that // the field resolution works in the Calendar. 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; } } } if (weekDate) { int weekOfYear = isSet(WEEK_OF_YEAR) ? field[MAX_FIELD + WEEK_OF_YEAR] : 1; int dayOfWeek = isSet(DAY_OF_WEEK) ? field[MAX_FIELD + DAY_OF_WEEK] : cal.getFirstDayOfWeek(); if (!isValidDayOfWeek(dayOfWeek) && cal.isLenient()) { if (dayOfWeek >= 8) { dayOfWeek--; weekOfYear += dayOfWeek / 7; dayOfWeek = (dayOfWeek % 7) + 1; } else { while (dayOfWeek <= 0) { dayOfWeek += 7; weekOfYear--; } } dayOfWeek = toCalendarDayOfWeek(dayOfWeek); } cal.setWeekDate(field[MAX_FIELD + WEEK_YEAR], weekOfYear, dayOfWeek); } return cal; }

      在CalendarBuilder.establish()方法中先后調(diào)用了cal.clear()與cal.set(),也就是先清除cal對象中設(shè)置的值,再重新設(shè)置新的值。由于Calendar內(nèi)部并沒有線程安全機(jī)制,并且這兩個操作也都不是原子性的,所以當(dāng)多個線程同時操作一個SimpleDateFormat時就會引起cal的值混亂。類似地, format()方法也存在同樣的問題。

      因此, SimpleDateFormat類不是線程安全的根本原因是:DateFormat類中的Calendar對象被多線程共享,而Calendar對象本身不支持線程安全。

      那么,得知了SimpleDateFormat類不是線程安全的,以及造成SimpleDateFormat類不是線程安全的原因,那么如何解決這個問題呢?接下來,我們就一起探討下如何解決SimpleDateFormat類在高并發(fā)場景下的線程安全問題。

      解決SimpleDateFormat類的線程安全問題

      解決SimpleDateFormat類在高并發(fā)場景下的線程安全問題可以有多種方式,這里,就列舉幾個常用的方式供參考,大家也可以在評論區(qū)給出更多的解決方案。

      1.局部變量法

      最簡單的一種方式就是將SimpleDateFormat類對象定義成局部變量,如下所示的代碼,將SimpleDateFormat類對象定義在parse(String)方法的上面,即可解決問題。

      package io.binghe.concurrent.lab06; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author binghe * @version 1.0.0 * @description 局部變量法解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest02 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); simpleDateFormat.parse("2020-01-01"); } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號量發(fā)生錯誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }

      此時運(yùn)行修改后的程序,輸出結(jié)果如下所示。

      所有線程格式化日期成功

      至于在高并發(fā)場景下使用局部變量為何能解決線程的安全問題,會在【JVM專題】的JVM內(nèi)存模式相關(guān)內(nèi)容中深入剖析,這里不做過多的介紹了。

      當(dāng)然,這種方式在高并發(fā)下會創(chuàng)建大量的SimpleDateFormat類對象,影響程序的性能,所以,這種方式在實際生產(chǎn)環(huán)境不太被推薦。

      2.synchronized鎖方式

      將SimpleDateFormat類對象定義成全局靜態(tài)變量,此時所有線程共享SimpleDateFormat類對象,此時在調(diào)用格式化時間的方法時,對SimpleDateFormat對象進(jìn)行同步即可,代碼如下所示。

      package io.binghe.concurrent.lab06; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author binghe * @version 1.0.0 * @description 通過Synchronized鎖解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest03 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; //SimpleDateFormat對象 private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { synchronized (simpleDateFormat){ simpleDateFormat.parse("2020-01-01"); } } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號量發(fā)生錯誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }

      此時,解決問題的關(guān)鍵代碼如下所示。

      synchronized (simpleDateFormat){ simpleDateFormat.parse("2020-01-01"); }

      運(yùn)行程序,輸出結(jié)果如下所示。

      所有線程格式化日期成功

      需要注意的是,雖然這種方式能夠解決SimpleDateFormat類的線程安全問題,但是由于在程序的執(zhí)行過程中,為SimpleDateFormat類對象加上了synchronized鎖,導(dǎo)致同一時刻只能有一個線程執(zhí)行parse(String)方法。此時,會影響程序的執(zhí)行性能,在要求高并發(fā)的生產(chǎn)環(huán)境下,此種方式也是不太推薦使用的。

      3.Lock鎖方式

      Lock鎖方式與synchronized鎖方式實現(xiàn)原理相同,都是在高并發(fā)下通過JVM的鎖機(jī)制來保證程序的線程安全。通過Lock鎖方式解決問題的代碼如下所示。

      package io.binghe.concurrent.lab06; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @author binghe * @version 1.0.0 * @description 通過Lock鎖解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest04 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; //SimpleDateFormat對象 private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); //Lock對象 private static Lock lock = new ReentrantLock(); public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { lock.lock(); simpleDateFormat.parse("2020-01-01"); } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }finally { lock.unlock(); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號量發(fā)生錯誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }

      通過代碼可以得知,首先,定義了一個Lock類型的全局靜態(tài)變量作為加鎖和釋放鎖的句柄。然后在simpleDateFormat.parse(String)代碼之前通過lock.lock()加鎖。這里需要注意的一點(diǎn)是:為防止程序拋出異常而導(dǎo)致鎖不能被釋放,一定要將釋放鎖的操作放到finally代碼塊中,如下所示。

      finally { lock.unlock(); }

      運(yùn)行程序,輸出結(jié)果如下所示。

      所有線程格式化日期成功

      此種方式同樣會影響高并發(fā)場景下的性能,不太建議在高并發(fā)的生產(chǎn)環(huán)境使用。

      4.ThreadLocal方式

      使用ThreadLocal存儲每個線程擁有的SimpleDateFormat對象的副本,能夠有效的避免多線程造成的線程安全問題,使用ThreadLocal解決線程安全問題的代碼如下所示。

      package io.binghe.concurrent.lab06; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author binghe * @version 1.0.0 * @description 通過ThreadLocal解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest05 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; private static ThreadLocal threadLocal = new ThreadLocal(){ @Override protected DateFormat initialValue() { return new SimpleDateFormat("yyyy-MM-dd"); } }; public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { threadLocal.get().parse("2020-01-01"); } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號量發(fā)生錯誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }

      通過代碼可以得知,將每個線程使用的SimpleDateFormat副本保存在ThreadLocal中,各個線程在使用時互不干擾,從而解決了線程安全問題。

      運(yùn)行程序,輸出結(jié)果如下所示。

      所有線程格式化日期成功

      此種方式運(yùn)行效率比較高,推薦在高并發(fā)業(yè)務(wù)場景的生產(chǎn)環(huán)境使用。

      另外,使用ThreadLocal也可以寫成如下形式的代碼,效果是一樣的。

      package io.binghe.concurrent.lab06; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author binghe * @version 1.0.0 * @description 通過ThreadLocal解決SimpleDateFormat類的線程安全問題 */ public class SimpleDateFormatTest06 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; private static ThreadLocal threadLocal = new ThreadLocal(); private static DateFormat getDateFormat(){ DateFormat dateFormat = threadLocal.get(); if(dateFormat == null){ dateFormat = new SimpleDateFormat("yyyy-MM-dd"); threadLocal.set(dateFormat); } return dateFormat; } public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { getDateFormat().parse("2020-01-01"); } catch (ParseException e) { System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); }catch (NumberFormatException e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號量發(fā)生錯誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }

      5.DateTimeFormatter方式

      DateTimeFormatter是Java8提供的新的日期時間API中的類,DateTimeFormatter類是線程安全的,可以在高并發(fā)場景下直接使用DateTimeFormatter類來處理日期的格式化操作。代碼如下所示。

      package io.binghe.concurrent.lab06; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author binghe * @version 1.0.0 * @description 通過DateTimeFormatter類解決線程安全問題 */ public class SimpleDateFormatTest07 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { LocalDate.parse("2020-01-01", formatter); }catch (Exception e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號量發(fā)生錯誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }

      可以看到,DateTimeFormatter類是線程安全的,可以在高并發(fā)場景下直接使用DateTimeFormatter類來處理日期的格式化操作。

      運(yùn)行程序,輸出結(jié)果如下所示。

      所有線程格式化日期成功

      使用DateTimeFormatter類來處理日期的格式化操作運(yùn)行效率比較高,推薦在高并發(fā)業(yè)務(wù)場景的生產(chǎn)環(huán)境使用。

      6.joda-time方式

      joda-time是第三方處理日期時間格式化的類庫,是線程安全的。如果使用joda-time來處理日期和時間的格式化,則需要引入第三方類庫。這里,以Maven為例,如下所示引入joda-time庫。

      joda-time joda-time 2.9.9

      引入joda-time庫后,實現(xiàn)的程序代碼如下所示。

      package io.binghe.concurrent.lab06; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; /** * @author binghe * @version 1.0.0 * @description 通過DateTimeFormatter類解決線程安全問題 */ public class SimpleDateFormatTest08 { //執(zhí)行總次數(shù) private static final int EXECUTE_COUNT = 1000; //同時運(yùn)行的線程數(shù)量 private static final int THREAD_COUNT = 20; private static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd"); public static void main(String[] args) throws InterruptedException { final Semaphore semaphore = new Semaphore(THREAD_COUNT); final CountDownLatch countDownLatch = new CountDownLatch(EXECUTE_COUNT); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < EXECUTE_COUNT; i++){ executorService.execute(() -> { try { semaphore.acquire(); try { DateTime.parse("2020-01-01", dateTimeFormatter).toDate(); }catch (Exception e){ System.out.println("線程:" + Thread.currentThread().getName() + " 格式化日期失敗"); e.printStackTrace(); System.exit(1); } semaphore.release(); } catch (InterruptedException e) { System.out.println("信號量發(fā)生錯誤"); e.printStackTrace(); System.exit(1); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); System.out.println("所有線程格式化日期成功"); } }

      這里,需要注意的是:DateTime類是org.joda.time包下的類,DateTimeFormat類和DateTimeFormatter類都是org.joda.time.format包下的類,如下所示。

      import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter;

      【高并發(fā)】SimpleDateFormat類的線程安全問題和解決方案(附6種解決方案)

      運(yùn)行程序,輸出結(jié)果如下所示。

      所有線程格式化日期成功

      使用joda-time庫來處理日期的格式化操作運(yùn)行效率比較高,推薦在高并發(fā)業(yè)務(wù)場景的生產(chǎn)環(huán)境使用。

      解決SimpleDateFormat類的線程安全問題的方案總結(jié)

      綜上所示:在解決解決SimpleDateFormat類的線程安全問題的幾種方案中,局部變量法由于線程每次執(zhí)行格式化時間時,都會創(chuàng)建SimpleDateFormat類的對象,這會導(dǎo)致創(chuàng)建大量的SimpleDateFormat對象,浪費(fèi)運(yùn)行空間和消耗服務(wù)器的性能,因為JVM創(chuàng)建和銷毀對象是要耗費(fèi)性能的。所以,不推薦在高并發(fā)要求的生產(chǎn)環(huán)境使用。

      synchronized鎖方式和Lock鎖方式在處理問題的本質(zhì)上是一致的,通過加鎖的方式,使同一時刻只能有一個線程執(zhí)行格式化日期和時間的操作。這種方式雖然減少了SimpleDateFormat對象的創(chuàng)建,但是由于同步鎖的存在,導(dǎo)致性能下降,所以,不推薦在高并發(fā)要求的生產(chǎn)環(huán)境使用。

      ThreadLocal通過保存各個線程的SimpleDateFormat類對象的副本,使每個線程在運(yùn)行時,各自使用自身綁定的SimpleDateFormat對象,互不干擾,執(zhí)行性能比較高,推薦在高并發(fā)的生產(chǎn)環(huán)境使用。

      DateTimeFormatter是Java 8中提供的處理日期和時間的類,DateTimeFormatter類本身就是線程安全的,經(jīng)壓測,DateTimeFormatter類處理日期和時間的性能效果還不錯(后文單獨(dú)寫一篇關(guān)于高并發(fā)下性能壓測的文章)。所以,推薦在高并發(fā)場景下的生產(chǎn)環(huán)境使用。

      joda-time是第三方處理日期和時間的類庫,線程安全,性能經(jīng)過高并發(fā)的考驗,推薦在高并發(fā)場景下的生產(chǎn)環(huán)境使用。

      好了,今天就到這兒吧,我是冰河,大家有啥問題可以在下方留言,也可以加我微信:hacker_binghe,我拉你進(jìn)群,一起交流技術(shù),一起進(jìn)階,一起牛逼~~

      Java JDK JVM 任務(wù)調(diào)度 多線程

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:我有三個連續(xù)的表格,文字內(nèi)容比較多,現(xiàn)在wps表格會自動把第三個表格分到第二頁。如果讓第三個表格變成
      下一篇:oracle asm 磁盤組信息查詢相關(guān)sql
      相關(guān)文章
      久久久久亚洲AV成人片| 亚洲色偷偷色噜噜狠狠99网| 亚洲综合色一区二区三区| 亚洲嫩草影院久久精品| 亚洲色偷偷综合亚洲AVYP| 亚洲 国产 图片| 亚洲AV伊人久久青青草原| 精品久久久久久亚洲综合网| 亚洲AV无码专区在线厂| 在线视频亚洲一区| 亚洲国产人成中文幕一级二级| 亚洲高清无码在线观看| 亚洲精品成人网久久久久久| 亚洲精品成人a在线观看| 亚洲精品线路一在线观看| 国产午夜亚洲精品理论片不卡| 亚洲熟妇无码另类久久久| 亚洲乱码中文字幕久久孕妇黑人| 亚洲国产精品福利片在线观看| 亚洲va久久久噜噜噜久久狠狠| 无码专区—VA亚洲V天堂| 亚洲男人天堂2017| 亚洲白色白色在线播放| 亚洲丝袜中文字幕| 亚洲日韩AV一区二区三区四区| 亚洲国产成人精品无码区花野真一 | 456亚洲人成影院在线观| 国产亚洲福利在线视频| 亚洲狠狠婷婷综合久久| 亚洲av无码乱码在线观看野外 | 亚洲精品无码久久久久久久| 亚洲国产精品乱码在线观看97| 亚洲av无码不卡久久| 亚洲精品无码mⅴ在线观看| 在线观看亚洲视频| 久久99亚洲综合精品首页| 国产精品亚洲精品日韩已满| 亚洲一区中文字幕久久| 亚洲AV色吊丝无码| 国产亚洲女在线线精品| 亚洲精品乱码久久久久久 |