Shekhar Gulati is a Java Consultant with over 5 years experience.He is currently working with Xebia India an Agile Software Development company.The postings on this site and on his blog are his own and do not necessarily represent opinion of his employer. His own blog is at http://whyjava.wordpress.com/ and you can follow him on twitter here. Shekhar has posted 25 posts at DZone. View Full User Profile

CheckThread - A Static Analysis Tool For Catching Java Concurrency Bugs

09.21.2010
| 18189 views |
  • submit to reddit

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.

Prerequisite

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.

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

}
}
int threadCount = 2000;
MyThread[] myThreads = new MyThread[threadCount];
for (int i = 0; i < myThreads.length; i++) {
myThreads[i] = new MyThread();
myThreads[i].start();
}
latch.countDown();
for (MyThread myThread : myThreads) {
myThread.join();
}
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>();

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

Published at DZone with permission of its author, Shekhar Gulati.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)