5

I am trying to apply the lessons from reading Java Concurrency In Practice with regards to declaring whether classes that I write are either thread-safe or that they contain unsynchronised mutable state. I think this is a good idea because it documents the intention of how the class should be used.

Today, I wrote a class which wraps an instance of java.lang.Class and java.net.URI. I was about to write in the javadoc that it is thread-safe immutable since both fields were declared as final references. However, I looked at the source code for URI and Class and didn't see any declaration of whether they are thread-safe and it didn't seem immediately obvious.

Thinking about this more generally: is there a list of common java classes stating whether they are thread-safe or not?

On the other had, it probably doesn't matter whether the instances are strictly thread-safe because of the way this class is being used, I will mark it as 'probably thread-safe' for now.

Matt
  • 557
  • 9
  • 17
  • There are a few well-known thread-unsafe classes, such as SimpleDateFormat for example, but I don't think there is an exhaustive, official list. So unless a class is documented as being safe or you can prove with confidence that it is and will *remain* safe in the foreseeable future, you should probably err on the cautious side... – assylias Jun 04 '13 at 10:48
  • well, did you ever find such a list of thread-safe classes? – Mike Nakis Jun 05 '22 at 21:59
  • Don't think it exists and you have to look into the source of the library classes you use – Matt Jun 07 '22 at 11:48

3 Answers3

6

There is no definitive list of thread-safe classes in Java. The only people who could produce a definitive list would be Oracle1, and they haven't done so.


1 - Oracle is the custodian of both the reference Java class libraries (including javadocs), other "official" documentation and .. the official compliance test suite for Java. They are the people who would say whether a class (or subset of a class) that is thread-safe, is thread-safe by design, or whether it is merely a implementation artefact. Nobody else can make that calll with total certainty; i.e. whether a class that is thread-safe in the standard Oracle codebase should also be thread-safe in Harvest / Android codebase or the Classpath codebase, or ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • What about apache commons classes for instance, which of THOSE are threadsafe? Apart from threshold, it looks like LevenshteinDistance is thread safe for instance. With some tweaks it could be made threadsafe. – ggb667 Feb 13 '23 at 21:40
  • I doubt that anyone has made a list of Apache Commons thread-safe classes. But ... hey ... Google is your friend. If you can find it, it exists. If you think this kind of thing is worth doing, >you< could analyze the thread safety of the Apache commons classes and publish it yourself. – Stephen C Feb 14 '23 at 02:20
  • Is there a tool to automate that and maybe determine that a thing is or is not thread safe? Seems pretty doable. – ggb667 Feb 14 '23 at 14:08
  • AFAIK, no there is no such tool. It is not as easy as you think. Sure ... it should be easy to detect some code that is obviously not thread-safe, and some other code that is obviously thread-safe. But as lot of code fall outside of those sets. Not least because the theoretical definition of thread-safety presupposes a (formal) specification of the behavior of the class / classes under examination. – Stephen C Feb 14 '23 at 15:24
5

All immutable classes are assumed to be thread-safe. For mutable classes the common practice is to state explicitly when the class is thread-safe; if nothing is stated, you can't assume thread safety.

I have never heard of a list of thread-safe JDK classes, though.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • +1 be warned that while some classes are considered thread safe, this doesn't mean you can use them any way you want and still be thread safe. e.g. you can't iterate over Vector without explicitly locking it. – Peter Lawrey Jun 04 '13 at 18:20
  • Also, you can't safely share them in a data race. – Marko Topolnik Jun 04 '13 at 19:26
5

All java.lang classes (As Marko said immutable classes are assumed to be thread-safe). Also BigDecimal and BigInteger and few more. Unfortunately there is no list of that classes.

If you need some thread-safe collections try Google Guava Immutable Collections (http://code.google.com/p/guava-libraries/wiki/GuavaExplained?tm=6)

Daniel Pokusa
  • 236
  • 1
  • 10