当前位置:网站首页>关于DateUtil时间工具类造成程序报错
关于DateUtil时间工具类造成程序报错
2022-04-23 21:59:00 【健康平安的活着】
一 问题描述
DateUtils工具类中,调取方法中格式化日期类SimpleDateFormat和calendar都被定义成静态成员变量。如下图所示:
定义一个线程池,模拟10个线程并发,调用此工具类:不同子线程调取不同月份,可以看到202209和202210两个月份返回结果一样,数据错位,变乱。应该2022209月份的当月最后一天是:Mon Sep 31 23:59:59 CST 2022。
二 问题解决办法
- 解决办法:将DateUtils工具类中,调取方法里格式化日期类SimpleDateFormat和calendar都被定义成局部变量。
经过多次执行,输入出结果都正确!
三 结论
DateUtils工具类中定义了全局静态的Calendar类和格式化日期类SimpleDateFormat类,并且在方法内直接使用了该calendar类来进行set、get等一系列操作和直接使用SimpleDateFormat类进行格式化日期。当并发多个请求在执行过程中,请求读写同一个calendar和SimpleDateFormat类,导致各个线程在getTime的时候不能够取到正确的时间以及程序报错。
需要在DateUtils类中,在使用的方法里通过重新定义一个Calendar类和new一个格式化日期类SimpleDateFormat类的对象。即 这两个类的全局变量改为局部变量。
版权声明
本文为[健康平安的活着]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u011066470/article/details/124365148
边栏推荐
- [leetcode sword finger offer 28. Symmetric binary tree (simple)]
- [leetcode sword finger offer 58 - I. flip word order (simple)]
- Ribbon停止维护
- 手撕《Google SRE Book》
- Ali has another "against the sky" container framework! This kubernetes advanced manual is too complete
- YOLOv5 Unable to find a valid cuDNN algorithm to run convolution
- JUnit unit test ---- a good tool for testing a single method
- 服务注册中心和Ribbon组件回顾
- Pycharm download and installation
- Problem brushing plan -- dynamic programming (IV)
猜你喜欢
Ribbon负载均衡策略
OpenFeign之响应处理
Yolov5 NMS source code understanding
Database Experiment 3 data update experiment
How to make Jenkins job run automatically after startup
C# ftpHelper
MySQL 回表
[leetcode refers to offer 22. The penultimate node in the linked list (simple)]
[leetcode refers to offer 18. Delete the node of the linked list (simple)]
服务注册中心和Ribbon组件回顾
随机推荐
MySQL back to table
Code cloud download history version process
This paper solves the cross domain problem of browser
MySQL 回表
[leetcode refers to offer 25. Merge two sorted linked lists (simple)]
Express ③ (use express to write interface and cross domain related issues)
Devops and cloud computing
[leetcode refers to the maximum profit of offer 63. Stock (medium)]
C reads excel specific data into specific columns of DataGridView
在线Excel转CSV工具
[leetcode refers to offer 22. The penultimate node in the linked list (simple)]
[SDU chart team - core] enumeration of SVG attribute class design
Pyuninstaller package exe cannot find the source code when running, function error oserror: could not get source code
微服务系统中服务降级
阿里又一个“逆天”容器框架!这本Kubernetes进阶手册简直太全了
JS prototype and prototype chain
[leetcode refers to offer 21. Adjust the array order so that odd numbers precede even numbers (simple)]
OpenFeign的参数传递之数组和集合类型
OpenFeign组件的使用
Opencv application -- jigsaw puzzle