×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Do Not Assume SimpleDateFormat Is Thread-safe
 

Submitted by: Octavia Anghel(octaviaanghel@gmail.com)

Thursday Jan 11th 2018 by Octavia Anghel

Language: Java, Level: Beginner: - Learn why it is important to remember that sharing a SimpleDateFormat requires proper synchronization.

The code below is improper because it shares a static instance of a SimpledateFormat with any possible number of threads.

Public class Constants {
     Public static final SimpleDateFprmat date = new SimpleDateFormat("dd.MM.yyyy");
}

SimpleDateFormat is not thread-safe; if multiple threads concurrently use this object, the result is undefined and the output will be bizarre (from format and parse or even exceptions). This code also doesn't take the time zone into account and it is important to remember that sharing a SimpleDateFormat requires proper synchronization.

Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved