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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  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

Shekhar Gulati user avatar by
Shekhar Gulati
·
Sep. 21, 10 · Interview
Like (0)
Save
Tweet
Share
26.38K 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.

Popular on DZone

  • Cloud Performance Engineering
  • Configure Kubernetes Health Checks
  • Reconciling Java and DevOps with JeKa
  • Important Takeaways for PostgreSQL Indexes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: