Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronizing access to SimpleDateFormat

The javadoc for SimpleDateFormat states that SimpleDateFormat is not synchronized.

"Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally."

But what is the best approach to using an instance of SimpleDateFormat in a multi threaded environment. Here are a few options I have thought of, I have used options 1 and 2 in the past but I am curious to know if there are any better alternatives or which of these options would offer the best performance and concurrency.

Option 1: Create local instances when required

public String formatDate(Date d) {     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");     return sdf.format(d); } 

Option 2: Create an instance of SimpleDateFormat as a class variable but synchronize access to it.

private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public String formatDate(Date d) {     synchronized(sdf) {         return sdf.format(d);     } } 

Option 3: Create a ThreadLocal to store a different instance of SimpleDateFormat for each thread.

private ThreadLocal<SimpleDateFormat> tl = new ThreadLocal<SimpleDateFormat>(); public String formatDate(Date d) {     SimpleDateFormat sdf = tl.get();     if(sdf == null) {         sdf = new SimpleDateFormat("yyyy-MM-hh");         tl.set(sdf);     }     return sdf.format(d); } 
like image 666
3urdoch Avatar asked Nov 05 '10 16:11

3urdoch


People also ask

Is SimpleDateFormat deprecated?

Class SimpleDateFormat. Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.

What is the difference between DateTimeFormatter and SimpleDateFormat?

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

What is the difference between DateFormat and SimpleDateFormat?

The java SimpleDateFormat allows construction of arbitrary non-localized formats. The java DateFormat allows construction of three localized formats each for dates and times, via its factory methods.

Is SimpleDateFormat can be used in multithreading?

Java's SimpleDateFormat is not thread-safe, Use carefully in multi-threaded environments. SimpleDateFormat is used to format and parse dates in Java. You can create an instance of SimpleDateFormat with a date-time pattern like yyyy-MM-dd HH:mm:ss , and then use that instance to format and parse dates to/from string.


1 Answers

  1. Creating SimpleDateFormat is expensive. Don't use this unless it's done seldom.

  2. OK if you can live with a bit of blocking. Use if formatDate() is not used much.

  3. Fastest option IF you reuse threads (thread pool). Uses more memory than 2. and has higher startup overhead.

For applications both 2. and 3. are viable options. Which is best for your case depends on your use case. Beware of premature optimization. Only do it if you believe this is an issue.

For libraries that would be used by 3rd party I'd use option 3.

like image 76
Peter Knego Avatar answered Oct 06 '22 00:10

Peter Knego