Question

I'm writing a Nexus 7 app and have inherited a skeletal app. from another developer. I ran a findbugs analysis on it and findbugs warned about a static instance of SimpleDateFormat:

As the JavaDoc states, DateFormats are inherently unsafe for multithreaded use. The detector has found a call to an instance of DateFormat that has been obtained via a static field. This looks suspicous.

I don't have any explicit thread creation or background actions in my app. Therefore do I need to take the above warning into account, or can it be ignored? I am not sure of what threads actually run in an Android app (apart from the UI servicing thread).

The dateformat object is set in the constructor like this:

private static final SimpleDateFormat shortCsvDateFormat = new SimpleDateFormat("EEE MMM d yyyy");

Was it helpful?

Solution

Sorry, the 'not' got lost.

This shouldn't be a problem.

But of course, this is a possible but not very likely source of bugs. If you look at the documentation of SimpleDateFormat, you'll see that there are two methods to apply a new pattern.

So it's possible that one thread changes the pattern while another uses the old pattern. But that looks very unlikely given the name of the variable. (I assume the CSV pattern doesn't change during runtime.)

If the final variable is encapsulated in a CSVWriter class or something the possibility is even smaller.

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