Question

Recently I encountered many bugs because of SimpleDateFormat is not thread safe.

The reason I found out was it was not synchronized properly or new instances were not created for separate threads.

So I thought let's create a PMD/Checkstyle so that in future if anybody uses SimpleDateFormat he will get some kind of warning about that it's not thread safe.

I am new to Checkstyle, can anyone describe me how can I achieve this?

Was it helpful?

Solution

FindBugs can output warnings when a DateFormat is defined as a static field. But detecting non-thread-safe usage of a DateFormat is very difficult, because it would need to understand how an object is used. I don't think writing a CheckStyle rule for this is possible. At least not without detecting a whole lot of false positives.

Maybe you should just forbid direct usage of DateFormat, and wrap it into a custom class, or mandate the use Apache commons FastDateFormat, which is thread-safe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top