Home > Bad Code > @ThreadSafe javadoc annotation

@ThreadSafe javadoc annotation

Recently I came across this line of code:

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");

Unfortunately, this code belongs to a class whose instances will be used by multiple threads. Even though the API documentation for SimpleDateFormat mentions it being not thread safe, somehow it failed to get the developers attention.

As I got to think more about this, a @ThreadSafe JavaDoc element might be useful for API writers to convey their intent more clearly. Perhaps the generated JavaDoc for a thread safe class can use a different color or font making the distinction more visual.

Categories: Bad Code Tags:
  1. August 14th, 2008 at 10:38 | #1

    Hi Balaji,

    Did you ever find a sample of generated Javadoc that provides a visual indication — in the generated Javadoc — of the presence of the @ThreadSafe annotation on a Java class?

    Kind regards,

  2. Dan R.
    November 10th, 2011 at 10:04 | #2

    The canonical annotations for thread safety seem to be provided by the book Java Concurrency In Practice. You can view them here:

  1. No trackbacks yet.