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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Measuring the Impact of AI on Software Engineering Productivity
  • How to Convert XLS to XLSX in Java
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways
  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.3K 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!