淺談 SimpleDateFormat,第三方庫joda-time,JDK8提供時間類 之性能和線程安全

      網友投稿 630 2022-05-30

      文章目錄

      一、java.text.SimpledateFormat

      線程不安全,拋出異常寫法:

      線程安全寫法:

      二、joda-time

      三、java.time.format.DateTimeFormatter

      一、java.text.SimpleDateFormat

      java.text.SimpleDateFormat 的實例對象在多線程共享使用的時候會拋出轉換異常,正確的使用方法應該是采用堆棧封閉,將其作為方法內的局部變量而不是全局變量,在每次調用方法的時候才去創建一個SimpleDateFormat實例對象,這樣利用堆棧封閉就不會出現并發問題。

      線程不安全,拋出異常寫法:

      package com.nobody.part02; 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 Mr.nobody * @Description * @date 2020/9/17 */ public class SimpleDateFormatDemo { // 請求總數 private static int CLIENT_COUNT = 10000; // 并發線程數 private static int THREAD_COUNT = 500; // 全局變量,多線程訪問會線程不安全,拋異常 private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); public static void main(String[] args) throws InterruptedException { // 固定線程池 ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT); // 控制同一個時刻,只能有多少個線程同時運行指定代碼,即acquire和release之間的代碼 Semaphore semaphore = new Semaphore(THREAD_COUNT); CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT); for (int i = 0; i < CLIENT_COUNT; i++) { executorService.execute(() -> { try { semaphore.acquire(); simpleDateFormat.parse("20200917"); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } }); } // 等待所有請求執行完 countDownLatch.await(); // 關閉線程池 executorService.shutdown(); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      淺談 SimpleDateFormat,第三方庫joda-time,JDK8提供時間類 之性能和線程安全

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      線程安全寫法:

      package com.nobody.part02; 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 Mr.nobody * @Description * @date 2020/9/17 */ public class SimpleDateFormatDemo { // 請求總數 private static int CLIENT_COUNT = 10000; // 并發線程數 private static int THREAD_COUNT = 500; // 全局變量,多線程訪問會線程不安全,拋異常 //private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); public static void main(String[] args) throws InterruptedException { // 固定線程池 ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT); // 控制同一個時刻,只能有多少個線程同時運行指定代碼,即acquire和release之間的代碼 Semaphore semaphore = new Semaphore(THREAD_COUNT); CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT); for (int i = 0; i < CLIENT_COUNT; i++) { executorService.execute(() -> { try { semaphore.acquire(); // 局部變量,線程安全 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); simpleDateFormat.parse("20200917"); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } }); } // 等待所有請求執行完 countDownLatch.await(); // 關閉線程池 executorService.shutdown(); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      二、joda-time

      第三方庫 joda-time 的 DateTimeFormatter 類是線程安全的。

      package com.nobody.part02; 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 Mr.nobody * @Description * @date 2020/9/17 */ public class JodaTimeDemo { // 請求總數 private static int CLIENT_COUNT = 10000; // 并發線程數 private static int THREAD_COUNT = 500; // 全局變量,線程安全 private static DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyyMMdd"); public static void main(String[] args) throws InterruptedException { // 固定線程池 ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT); // 控制同一個時刻,只能有多少個線程同時運行指定代碼,即acquire和release之間的代碼 Semaphore semaphore = new Semaphore(THREAD_COUNT); CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT); for (int i = 0; i < CLIENT_COUNT; i++) { executorService.execute(() -> { try { semaphore.acquire(); DateTime.parse("20200917", dateTimeFormatter).toDate(); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } }); } // 等待所有請求執行完 countDownLatch.await(); // 關閉線程池 executorService.shutdown(); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      三、java.time.format.DateTimeFormatter

      JDK8提供了許多不可變且線程安全的類,例如 java.time.LocalDate 、java.time.LocalTime,java.time.LocalDateTime 以及java.time.format.DateTimeFormatter等等。

      package com.nobody.part02; 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 Mr.nobody * @Description * @date 2020/9/17 */ public class JDK8TimeDemo { // 請求總數 private static int CLIENT_COUNT = 10000; // 并發線程數 private static int THREAD_COUNT = 500; // 全局變量,JDK8時間類,線程安全 private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd"); public static void main(String[] args) throws InterruptedException { // 固定線程池 ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_COUNT); // 控制同一個時刻,只能有多少個線程同時運行指定代碼,即acquire和release之間的代碼 Semaphore semaphore = new Semaphore(THREAD_COUNT); CountDownLatch countDownLatch = new CountDownLatch(CLIENT_COUNT); for (int i = 0; i < CLIENT_COUNT; i++) { executorService.execute(() -> { try { semaphore.acquire(); System.out.println(LocalDate.parse("20200917", dateTimeFormatter)); semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } finally { countDownLatch.countDown(); } }); } // 等待所有請求執行完 countDownLatch.await(); // 關閉線程池 executorService.shutdown(); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      線程安全,正常輸出

      JDK 任務調度

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:華為發布面向2025十大趨勢
      下一篇:華為云可信智能計算服務TICS,安全釋放數據價值
      相關文章
      青青青国产色视频在线观看国产亚洲欧洲国产综合 | 亚洲国产香蕉人人爽成AV片久久 | 亚洲www77777| 亚洲性色高清完整版在线观看| 久久国产亚洲高清观看| 亚洲精品国产成人专区| 亚洲av永久无码制服河南实里| 亚洲乱码中文字幕久久孕妇黑人 | 亚洲精品无码久久久久去q| 国产亚洲自拍一区| 亚洲伊人色欲综合网| 中文字幕精品亚洲无线码一区应用| 亚洲人成国产精品无码| 亚洲午夜爱爱香蕉片| 精品亚洲成α人无码成α在线观看 | 中文字幕乱码亚洲精品一区| 亚洲 日韩经典 中文字幕| 最新亚洲春色Av无码专区| 亚洲国产精品精华液| 亚洲av纯肉无码精品动漫| 精品亚洲视频在线| 亚洲AⅤ优女AV综合久久久| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲伊人久久大香线蕉在观| 国产成人精品日本亚洲11| 亚洲人成网站在线在线观看| 精品无码专区亚洲| 亚洲av无码国产精品色在线看不卡| 亚洲国产成人久久综合区| 国产亚洲精品精品国产亚洲综合| 伊人婷婷综合缴情亚洲五月| 亚洲啪啪AV无码片| 亚洲精品国产品国语在线| 亚洲日本中文字幕| 亚洲不卡视频在线观看| 亚洲中文字幕一区精品自拍| 激情小说亚洲色图| 亚洲中文久久精品无码| 亚洲国产精品无码久久一线| 久久亚洲AV无码精品色午夜麻豆| 亚洲jjzzjjzz在线观看|