DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mastering Unit Testing and Test-Driven Development in Java
  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Improving Java Code Security
  • Hints for Unit Testing With AssertJ

Trending

  • RAG Is Not Enough: Advanced Retrieval Architectures Using Vertex AI Search on GCP
  • Feature Flag Debt: Performance Impact in Enterprise Applications
  • Bringing Intelligence Closer to the Source: Why Real-Time Processing is the Heart of Edge AI
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  1. DZone
  2. Coding
  3. Java
  4. CheckThread - A Static Analysis Tool For Catching Java Concurrency Bugs

CheckThread - A Static Analysis Tool For Catching Java Concurrency Bugs

By 
Shekhar Gulati user avatar
Shekhar Gulati
·
Sep. 21, 10 · Interview
Likes (0)
Comment
Save
Tweet
Share
27.7K Views

Join the DZone community and get the full member experience.

Join For Free

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.

unit test Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Unit Testing and Test-Driven Development in Java
  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Improving Java Code Security
  • Hints for Unit Testing With AssertJ

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook