当前位置:网站首页>About the program error caused by dateutil time tool class

About the program error caused by dateutil time tool class

2022-04-23 22:06:00 Live healthy and safe

One Problem description

DateUtils In the tool class , Call the format date class in the method SimpleDateFormat and calendar Are defined as static member variables . As shown in the figure below :

Define a thread pool , simulation 10 Threads are concurrent , Call this tool class : Different sub threads call different months , can To see 202209 and 202210 The results returned in two months are the same , Data dislocation , Chaos . should 2022209 The last day of the month is :Mon Sep 31 23:59:59 CST 2022.

  Two The solution to the problem

  1. terms of settlement : take DateUtils In the tool class , Retrieval method in Format date class SimpleDateFormat and calendar Are defined as local variables .

After many executions , The input results are correct !

3、 ... and   Conclusion

DateUtils The tool class defines global static Calendar class And formatting date classes SimpleDateFormat class , And the... Is used directly within the method calendar Class set、get A series of operations And direct use SimpleDateFormat class Format date . When multiple concurrent requests are being executed , Request to read and write the same calendar and SimpleDateFormat class , Cause each thread to be in getTime You can't get the right time when And program error reporting .

Need to be in DateUtils Class , In the method used by Redefining a Calendar class and new One Format date class SimpleDateFormat class The object of . namely   Change the global variables to these two local variables .

版权声明
本文为[Live healthy and safe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/113/202204232159177562.html