January 17, 2014

When I first heard about ThreadSafe I first thought “damn, one more static code analysis tool on the market“. After using tools like PMD or FindBugs in our in-house development and after spending considerable amount of time to achieve the zero-warning policy, I was pretty sure there is no reason to use yet another tool. Well, I was terribly wrong.

The ThreadSafe is in many ways similar to other static analysis tools, except it’s strongly focused to one very important aspect of Java programming – concurrency. The guys from Contemplate did a great job in recognizing the need for a tool that can quickly point to possible problems in the code related to concurrency and multi-threading.

The tool can be used in two ways – as Eclipse plug-in or as Sonar plug-in. I tested the Eclipse plug-in and tried to analyze the source code of inspectIT, the free performance diagnose tool we develop in-house. I needed less than 3 minutes to install the plug-in, run the analysis on the complete source code and get valuable results.

The first impression I had when I was looking to the results was “hey, these warnings really have sense“. I hate the tools that report dozen of warnings, but only few of them are reasonable. The ThreadSafe defines total of 18 rules at the moment and all of the rules are defined carefully and smartly. In addition every single rule is very well documented and everybody can easily understand what a concrete warning means.

In our project the ThreadSafe found 44 potential problems divided into 9 problem types. The highest number of warnings were related to Inconsistent synchronization (15) and Non atomic use of Get/Check/Put (10). We did not need to much time to fix the problems, cause once you know where exactly they are and what can be the solution you can do it fast.

To demonstrate one example of using the ThreadSafe, I took a screen shot of the problem description in one of our classes and pasted the related source code:

Example of a ThreadSafe warning

As you can see, we easily found a class that instantiates a thread-safe list for one field, but also defines a public setter for that field that accepts list interface as parameter. Meaning that our thread-safe list could easily be substituted by any kind of list implementation, which would lead to synchronization issues due to the fact that class expects to work on thread-safe one. Thanks to the ThreadSafe we were able to fix this problem and many more.

I think that correct usage of concurrency is fundamental for high-performance applications and I believe that using ThreadSafe can considerably decrease the risk of wrong implementations of concurrency. Thus, each project should consider having a tool like ThreadSafe to prevent problems occurring even in an early development phase.

Leave a Comment


  1. D

    There was an article in InfoQ this week showing examples of Java concurrency errors that ThreadSafe finds in open source applications including Apache JMeter: http://www.infoq.com/articles/Java-Concurrency-Static-Analysis-with-ThreadSafe

  2. D

    There is now a command-line interface to ThreadSafe which makes it available to all developers irrespective of IDE, and for use with build/CI tools: http://www.contemplateltd.com/news/threadsafe-1-3

  3. B

    I did some basic tests with a HashMap trying to do put if absent operation , which is non-atomic and I expected the tool to flag it out. Surprisingly , it did not flag it. Disappointed.

  4. I

    I am wandering why would the tool flag the HashMap when it considered not thread-safe. Actually it’s very good that it did not flag it, just imagine how bad would be that the tool just assumes that complete code you write must be thread-safe.

    My impression is that if you are using HashMap you don’t want this peace of code to be thread-safe and thus the tool ignores it. On the other hand if you are using ConcurrentHashMap (thus implicitly saying this code should be thread safe) and don’t use putIfAbsent properly the tool will issue a warning. That’s exactly what I expect from the tool like this.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.