How to understand SimpleDateFormat
How to understand SimpleDateFormat, in view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Testpublic void testParse () {ExecutorService executorService = Executors.newCachedThreadPool (); List dateStrList = Lists.newArrayList ("2018-04-01 10:00:01", "2018-04-02 11:00:02", "2018-04-03 12:00:03", "2018-04-04 13:00:04", "2018-04-05 14:00:05") / * note that this place is easy to make mistakes! * so SimpleDateFormat objects cannot be shared here! * / SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss"); for (String str: dateStrList) {executorService.execute (()-> {try {simpleDateFormat.parse (str); TimeUnit.SECONDS.sleep (1);} catch (Exception e) {e.printStackTrace ();}}) }
The parse method of using SimpleDateFormat in a concurrent environment has thread safety problems!
Causes of thread safety problems:
If thread An empties the calendar at this time and does not set a new value
Thread B also enters the parse method using the calendar object in the SimpleDateFormat object.
Thread safety problems will arise at this point!
The SimpleDateFormat conversion date is manipulated through the Calendar object
The answer to the question on how to understand SimpleDateFormat is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.