淺談 SimpleDateFormat,第三方庫joda-time,JDK8提供時間類 之性能和線程安全
文章目錄
一、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
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小時內刪除侵權內容。