Over a million developers have joined DZone.

CheckThread - A Static Analysis Tool For Catching Java Concurrency Bugs

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

A few days back, I was browsing the web and found an interesting open source framework called CheckThread.  It is a static analysis tool for catching Java concurrency bugs at compile time. Static analysis tools are used to find out programming error in the code by analyzing their byte code. To me a tool aiming to catch concurrency bugs at compile time was worth spending time on. So, I decided to play with checkthread to find out its capabilities.

CheckThread requires developers to specify the thread policy in either xml or annotations. Thread policy defines whether the piece of code (i.e. a method) is thread safe, not thread safe, or thread confined. Thread confined means that this method is confined to a specific runtime thread. For example, the Swing API must be invoked on the Event-Dispatch thread.


Before you start:

  1. Download Eclipse Plugin. Put plugin in jar in Eclipse plugins folder and restart Eclipse. For more information refer here.
  2. Download Checkthread annotation jar. This jar is not present in any maven repository, so manually install the jar in your maven repository.
    mvn install:install-file -Dfile=checkthread-annotations-1.0.9.jar -DgroupId=org.checkthread -DartifactId=checkthread-annotations -Dversion=1.0.9 -Dpackaging=jar

CheckThread Capabilities

Let's look at an example:

public class ThreadSafetyExample {

final Map<String, String> helperMap = new HashMap<String, String>();

public void addElementToMap() {
helperMap.put("name", "shekhar");



This is a simple class which puts a key value pair in a map. Is this code thread safe? No.

Lets test this class using multiple threads. In the unit test I am using CountDownLatch for producing maximum parallelism. I talked about CountDownLatch in an earlier article.

public void regressionTest() throws Exception{
for (int i = 1; i <= 100; i++) {
System.out.println("Runing "+i);
public void addElementToMapWhenAccessedByMultipleThread() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ThreadSafetyExample helper = new ThreadSafetyExample();
class MyThread extends Thread {
public void run() {
try {
} catch (InterruptedException e) {

int threadCount = 2000;
MyThread[] myThreads = new MyThread[threadCount];
for (int i = 0; i < myThreads.length; i++) {
myThreads[i] = new MyThread();
for (MyThread myThread : myThreads) {
assertEquals(1, helper.helperMap.size());

I am calling the method addElementToMapWhenAccessedByMultipleThread in a for loop because if you run the test only once it might not fail (Thread timing). So, how can these types of errors be detected at compile time if most of the unit-tests and integration tests that we write do not test the code in multi-threaded environments? CheckThread can help you out in such situations. CheckThread can only help you if you annotate your method with ThreadSafe annotation. For example, lets apply @ThreadSafe annotation to the ThreadSafetyExample class:

public class ThreadSafetyExample {

final Map<String, String> helperMap = new HashMap<String, String>();

public void addElementToMap() {
helperMap.put("name", "shekhar");

 Now when you run the CheckThread by pressing the  button you will see a compile time error as shown below:


As you can see in the above image, the tool shows the code where problems exist and descriptions of errors in the problems section. This can come in handy while writing multi threaded code. You just need to think about your contract, whether the method you have written should be thread safe or not.

This was just a simple use case but it can also help you detect race conditions. Have a look at this tool, maybe it can help you detect concurrency bugs.

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}