SimpleDateFormat thread safety issues and solutions

Source: Internet
Author: User

SimpleDateFormat is a common class in Java that is used to parse and format date strings, but it can cause very subtle and difficult-to-debug problems if you use carelessness.

Because the DateFormat and SimpleDateFormat classes are not thread-safe, calling the format () and Parse () methods in a multithreaded environment should use synchronous code to avoid problems.
Or you can use threadlocal to turn shared variables into exclusive, and thread-exclusive will definitely be able to reduce the overhead of creating objects in a concurrent environment in a concurrency-exclusive way.
This approach is generally recommended if performance requirements are high.

1  Public classDateutil {2 3     Static FinalDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");4 5     StaticThreadlocal<dateformat> ThreadLocal =NewThreadlocal<dateformat>() {6         protectedDateFormat InitialValue () {7             return NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");8         }9     };Ten  One     Private StaticDateFormat DateFormat () { A         //return SDF; -         returnthreadlocal.get (); -     } the  -      Public StaticDate Parse (String datestr)throwsParseException { -         returnDateFormat (). Parse (DATESTR); -     } +  -      Public StaticString Format (date date) { +         returnDateFormat (). Format (date); A     } at  -      Public Static voidMain (string[] args) { -          for(inti = 0; I < 3; i++) { -             NewThread () { -                  Public voidrun () { -                      while(true) { in                         Try { -Thread.Sleep (2000); to}Catch(interruptedexception E1) { + e1.printstacktrace (); -                         } the                          *                         Try { $System.out.println ( This. GetName () + ":" + dateutil.parse ("2013-05-24 06:02:20"));Panax Notoginseng}Catch(ParseException e) { - e.printstacktrace (); the                         } +                     } A                 } the }.start (); +         } -}

Reference:

Http://www.cnblogs.com/zemliu/p/3290585.html

Http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html

SimpleDateFormat thread safety issues and solutions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.