为什么SimpleDateFormat线程不安全
发布日期:2021-05-06 22:19:53 浏览次数:40 分类:精选文章

本文共 782 字,大约阅读时间需要 2 分钟。

往期精选

  ●  (4000G)

  ●  

  ●  互联网技术干货视频教程大全【菜单为准】

  ●  

  ●  

  ●   

  ●  

      更多精彩查看历史记录和公众号菜单.........

     

  • parse 方法为什么不线程安全

1.有一个共享变量calendar,而这个共享变量的访问没有做到线程安全

2.parse方法生成CalendarBuilder,然后通过CalendarBuilder 设值到calendar,最后calendar.getTime();

3.api方法说明 This parsing operation uses the calendar to produce a Date. All of the calendar's date-time fields are cleared before parsing, and the calendar's default values of the date-time fields are used for any missing date-time information.

  • format方法为什么不线程安全

1.有一个共享变量calendar,而这个共享变量的访问没有做到线程安全

2.当使用format方法时,实际是给calent共享变量设置date值,然后调用subFormat将date转化成字符串

--------------------

-------------------

  • 解决方案

1.创建一个共享的SimpleDateFormat实例变量,但是在使用的时候,需要对这个变量进行同步

2.使用ThreadLocal为每个线程都创建一个线程独享SimpleDateFormat变量

3.需要的时候创建局部变量

作者:LION

原文:https://www.cnblogs.com/lion88/p/6426019.html

上一篇:JAVA 中IO总结 同步、异步、阻塞、非阻塞
下一篇:RabbitMQ的应用场景总结

发表评论

最新留言

很好
[***.229.124.182]2025年03月28日 10时40分21秒