Is there a list of thread-safe classes in Java?
Asked Answered
M

3

6

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.

Monocarpic answered 4/6, 2013 at 10:44 Comment(3)
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...Ceasar
well, did you ever find such a list of thread-safe classes?Payer
Don't think it exists and you have to look into the source of the library classes you useMonocarpic
M
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 ...

Mindi answered 4/6, 2013 at 11:31 Comment(4)
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.Emptor
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.Mindi
Is there a tool to automate that and maybe determine that a thing is or is not thread safe? Seems pretty doable.Emptor
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.Mindi
L
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.

Lindeberg answered 4/6, 2013 at 10:49 Comment(2)
+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.Noxious
Also, you can't safely share them in a data race.Lindeberg
H
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)

Halfprice answered 4/6, 2013 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.