CheckThread - A Static Analysis Tool For Catching Java Concurrency Bugs
Join the DZone community and get the full member experience.
Join For Freea 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:
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.
Opinions expressed by DZone contributors are their own.
Comments