浅谈 SimpleDateFormat,第三方库joda-time,JDK8提供时间类 之性能和线程安全

网友投稿 474 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

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

浅谈 SimpleDateFormat,第三方库joda-time,JDK8提供时间类 之性能和线程安全

41

42

43

44

45

46

47

48

线程安全,正常输出

JDK 任务调度

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:记不住命令行?用这个开源项目一次性解决问题!
下一篇:华为云可信智能计算服务TICS,安全释放数据价值
相关文章