当前位置:网站首页>关于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
边栏推荐
- Database Experiment 3 data update experiment
- Detectron2 usage model
- Hystrix components
- 清华大学 | WebFace260M:百万级深度人脸识别的基准(TPAMI2022)
- FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
- 分布式系统服务熔断
- Swift import third-party library reports an error no such module““
- 延时消息常见实现方案
- Common scenes of vector product in image
- OpenFeign组件的简介和使用
猜你喜欢

MySQL back to table
![[leetcode refers to offer 21. Adjust the array order so that odd numbers precede even numbers (simple)]](/img/63/1701a93f91f792195a74edfb99fe18.png)
[leetcode refers to offer 21. Adjust the array order so that odd numbers precede even numbers (simple)]

Database Experiment four View experiment

Ribbon组件基本介绍和使用

Database Experiment 2 data query

How to make Jenkins job run automatically after startup

Two Stage Detection
![[※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]](/img/fa/99b64267fd890c6c40847548f077b3.png)
[※ leetcode refers to offer 48. The longest substring without repeated characters (medium)]

OpenFeign超时设置

C reads excel specific data into specific columns of DataGridView
随机推荐
RestTemplate 服务调用
Oracle ora-01033: Oracle initialization or shutdown in progressprocess solution
Preliminary analysis of Airbase
MVC three-tier architecture (illustration)
Hystrix断路器开启条件和流程以及默认备选处理
MySQL 回表
Cancel the default open project setting of idea
Plato Farm元宇宙IEO上线四大,链上交易颇高
不同注册中心区别
Based on jsplumb JS to achieve multi list one to many connection effect
服务间通信方式
Resolve the "chromedriver executable needs to be in path" error
[leetcode refers to offer 32 - III. print binary tree III from top to bottom (medium)]
从严从重从快 上海全面加强疫情期间食品安全监管
[※ leetcode refers to offer 46. Translate numbers into strings (medium)]
Database Experiment 8 trigger experiment
在线Excel转CSV工具
Thread safe sigleton (singleton mode)
服务降级的实现
Pycharm Chinese plug-in