Google GSON and Thread Safety

At work we are using gson to do some soft serialization between some applications. Our production application started to throw some errors this afternoon, and after looking into the problem it would appear that gson has a static reference to SimpleDateFormat (or, more correctly, a static member that was constructed with DateFormat.getDateTimeInstance(), which returns a SimpleDateFormat).  You can see the problem here, line 58. A bug report has been submitted. F or now, we have added our own type adaptor that uses a ThreadLocal to wrap SimpleDateFormat.

To Google's credit, they fixed the issue within 2 hours of our discovering it. They chose to just syncronize the methods (a little slower than using ThreadLocal).

Leave a Reply