Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 843
Matt Avatar asked Jun 04 '13 10:06

Matt


People also ask

How do I know if a class is thread-safe?

For a standard Java SE class, the best way to know whether or not the class is thread-safe is to carefully read its documentation. Always read both the class documentation and the method documentation. If either say it's not synchronized or not thread-safe, you know it's not thread-safe.

Are all built in Java classes thread-safe?

Vector class that shipped with the original version of Java has synchronized methods to guarantee thread safety. But most applications do not require thread safety, and Java 1.2 provided the more efficient unsynchronized alternative java.

Which class is not thread-safe in Java?

Since the ++ and -- operations are not atomic the class is not thread safe. Also, since count is static , modifying it from decrement which is a synchronized instance method is unsafe since it can be called on different instances and modify count concurrently that way.

Is ArrayList class thread-safe?

Vectors are synchronized. Any method that touches the Vector 's contents is thread safe. ArrayList , on the other hand, is unsynchronized, making them, therefore, not thread safe.


1 Answers

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 ...

like image 192
Stephen C Avatar answered Nov 15 '22 01:11

Stephen C