java的SimpleDateFormat线程不安全出问题了,项目不支持java8,虚竹教你这一招

网友投稿 646 2022-05-30

目录

一、场景

二、SimpledateFormat线程为什么是线程不安全的呢?

验证SimpleDateFormat线程不安全

三、FastDateFormat源码分析

实践

四、结论

一、场景

Java8以前,要格式化日期时间,就需要用到SimpleDateFormat。

但我们知道SimpleDateFormat是线程不安全的,处理时要特别小心,要加锁或者不能定义为static,要在方法内new出对象,再进行格式化。很麻烦,而且重复地new出对象,也加大了内存开销。

后来Apache 在commons-lang 包中扩展了FastDateFormat对象,它是一个线程安全的,可以用来完美替换SimpleDateFormat。

二、SimpleDateFormat线程为什么是线程不安全的呢?

来看看SimpleDateFormat的源码

// Called from Format after creating a FieldDelegate private StringBuffer format(Date date, StringBuffer toAppendTo, FieldDelegate delegate) { // Convert input date to time field list calendar.setTime(date); ... }

1

2

3

4

5

6

7

问题就出在成员变量calendar,如果在使用SimpleDateFormat时,用static定义,那SimpleDateFormat变成了共享变量。那SimpleDateFormat中的calendar就可以被多个线程访问到。

验证SimpleDateFormat线程不安全

public class SimpleDateFormatDemoTest { private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) { //1、创建线程池 ExecutorService pool = Executors.newFixedThreadPool(5); //2、为线程池分配任务 ThreadPoolTest threadPoolTest = new ThreadPoolTest(); for (int i = 0; i < 10; i++) { pool.submit(threadPoolTest); } //3、关闭线程池 pool.shutdown(); } static class ThreadPoolTest implements Runnable{ private volatile int i=0; @Override public void run() { while (i<10){ String dateString = simpleDateFormat.format(new Date()); try { Date parseDate = simpleDateFormat.parse(dateString); String dateString2 = simpleDateFormat.format(parseDate); System.out.println(Thread.currentThread().getName()+" : "+i++); System.out.println(dateString.equals(dateString2)); System.out.println("-------------------------"); } catch (ParseException e) { e.printStackTrace(); } } } } }

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

出现了两次false,说明线程是不安全的。

三、FastDateFormat源码分析

Apache Commons Lang 3.5

1

//FastDateFormat @Override public String format(final Date date) { return printer.format(date); } @Override public String format(final Date date) { final Calendar c = Calendar.getInstance(timeZone, locale); c.setTime(date); return applyRulesToString(c); }

1

2

3

4

5

6

7

8

9

10

11

12

源码中 Calender 是在 format 方法里创建的,肯定不会出现 setTime 的线程安全问题。这样线程安全疑惑解决了。那还有性能问题要考虑?

我们来看下FastDateFormat是怎么获取的

FastDateFormat.getInstance(); FastDateFormat.getInstance(CHINESE_DATE_TIME_PATTERN);

1

2

看下对应的源码

/** * 获得 FastDateFormat实例,使用默认格式和地区 * * @return FastDateFormat */ public static FastDateFormat getInstance() { return CACHE.getInstance(); } /** * 获得 FastDateFormat 实例,使用默认地区
* 支持缓存 * * @param pattern 使用{@link java.text.SimpleDateFormat} 相同的日期格式 * @return FastDateFormat * @throws IllegalArgumentException 日期格式问题 */ public static FastDateFormat getInstance(final String pattern) { return CACHE.getInstance(pattern, null, null); }

1

2

3

4

5

6

7

8

9

10

java的SimpleDateFormat线程不安全出问题了,项目不支持java8,虚竹教你这一招

11

12

13

14

15

16

17

18

19

20

这里有用到一个CACHE,看来用了缓存,往下看

private static final FormatCache CACHE = new FormatCache(){ @Override protected FastDateFormat createInstance(final String pattern, final TimeZone timeZone, final Locale locale) { return new FastDateFormat(pattern, timeZone, locale); } }; // abstract class FormatCache { ... private final ConcurrentMap cInstanceCache = new ConcurrentHashMap<>(7); private static final ConcurrentMap C_DATE_TIME_INSTANCE_CACHE = new ConcurrentHashMap<>(7); ... }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

在getInstance 方法中加了ConcurrentMap 做缓存,提高了性能。且我们知道ConcurrentMap 也是线程安全的。

实践

/** * 年月格式 {@link FastDateFormat}:yyyy-MM */ public static final FastDateFormat NORM_MONTH_FORMAT = FastDateFormat.getInstance(NORM_MONTH_PATTERN);

1

2

3

4

//FastDateFormatpublic static FastDateFormat getInstance(final String pattern) { return CACHE.getInstance(pattern, null, null);}

1

如图可证,是使用了ConcurrentMap 做缓存。且key值是格式,时区和locale(语境)三者都相同为相同的key。

四、结论

java8之前,可使用FastDateFormat 替换SimpleDateFormat,达到线程安全的目的;

java8及以上的,java8提供了一套新的日期时间API,可以使用DateTimeFormatter来代替SimpleDateFormat。具体的源码分析,可以看这里,传送门:万字博文教你搞懂java源码的日期和时间相关用法

Java 任务调度

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

上一篇:Linux之time命令
下一篇:SQL入门之概念篇--00
相关文章