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

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

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

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

  • The Ultimate Chaos Testing Guide
  • Chaos Engineering for Microservices
  • Shift-Right Testing: Smart Automation Through AI and Observability
  • Cost-Aware Resilience: Implementing Chaos Engineering Without Breaking the Budget

Trending

  • Testing SingleStore's MCP Server
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Why We Still Struggle With Manual Test Execution in 2025
  • Scalable System Design: Core Concepts for Building Reliable Software
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Chaos Engineering: Blocked Threads

Chaos Engineering: Blocked Threads

A thread will enter into a BLOCKED state when it can't acquire a lock on an object because another thread already holds the lock on the object and doesn’t release it.

By 
Ram Lakshmanan user avatar
Ram Lakshmanan
DZone Core CORE ·
May. 20, 21 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
10.4K Views

Join the DZone community and get the full member experience.

Join For Free

In the series of chaos engineering articles, we have been learning to simulate various performance problems. In this post, let’s discuss how to make threads go into a BLOCKED state.

Sample Program

Here is a sample program from the open-source BuggyApp application, which would make threads go into a BLOCKED state. A thread will enter into a BLOCKED state when it couldn’t acquire a lock on an object because another thread already holds the lock on the same object and doesn’t release it. Review the program carefully.

public class BlockedAppDemo {    
public static void start() {                 
               for (int counter = 0; counter < 10; ++counter) {                     
          // Launch 10 threads.           
            new AppThread().start();      
      }      
   } 
}
 public class AppThread extends Thread {    
@Override    
public void run() {          
       AppObject.getSomething();    
    } 
}
 public class AppObject {    
       public static synchronized void getSomething() {            
           while (true) {                
             try {            
          Thread.sleep(10 * 60 * 1000);              
          } catch (Exception e) {}     
       }        
    }
 }


The sample program contains the ‘BlockedAppDemo’ class. This class has start() method. In this method,’ 10 new threads are created.  In AppThread class there is a run() method that invokes getSomething() method on the AppObject. In this getSomething() method, a thread is put to continuous sleep, i.e., the thread is repeatedly sleeping for 10 minutes again and again. But if you notice, getSomething() method is a synchronized method. Synchronized methods can be executed by only one thread at a time. If any other thread tries to execute the getSomething() method while the previous thread is still working on the method, then the new thread will be put in the BLOCKED state. 

In this case, 10 threads are launched to execute getSomething() method. But however, only one thread will acquire a lock and execute this method, the remaining 9 threads will be put in the BLOCKED state.

NOTE: If threads are BLOCKED for a prolonged period, the application may become unresponsive.

How to Diagnose ‘Blocked Thread’? 

You can diagnose Blocked Thread either through a manual or automated approach.

Manual Approach

In the manual approach, you need to capture thread dumps as the first step. A thread dump shows all the threads that are in memory and their code execution path. You can capture thread dump using one of the 8 options mentioned here. But an important criteria is: You need to capture thread dump right when the problem is happening. Once the thread dump is captured, you need to import the thread dump from your production servers to your local machine. You can use thread dump analysis tools like fastThread, samurai to analyze the thread dumps from your local machine.

Automated Approach

You can use root cause analysis tools like yCrash, which automatically captures application-level data (thread dump, heap dump, Garbage Collection log) and system-level data (netstat, vmstat, iostat, top, top -H, dmesg,…). Besides capturing the data automatically, it marries application-level data and system-level data and generates an instant root cause analysis report. Below is the report generated by the yCrash tool when the above sample program is executed:

Reporting the Line of Code in Which 9 Threads Are in The Blocked State

Fig:  yCrash reporting the line of code in which 9 threads are in the blocked state.

You can notice the yCrash tool reporting 9 threads are in the BLOCKED state and it’s also pointing out the stack trace in which they are stuck. From the stacktrace you can observe that thread is stuck on ‘com.buggyapp.blockedapp.AppObject#getSomething()’ method.

yCrash Transitive Graph Showing BLOCKED Threads

Fig:  yCrash transitive graph showing BLOCKED threads.

yCrash prints a transitive dependency graph that shows what thread is blocking what threads. In this transitive graph, you can see ‘Thread-19’ blocking 9 other threads. If you click on the thread names in the graph, you can see the stack trace of that particular thread. When you click on ‘Thread-19’, you will notice that the thread is stuck on the sleep() method in java.lang.Thread. Stack trace of ‘Thread-19’ will also point out that before getting stuck, this thread has obtained 1 lock, and due to which 9 threads are put in the BLOCKED state.


Chaos engineering

Opinions expressed by DZone contributors are their own.

Related

  • The Ultimate Chaos Testing Guide
  • Chaos Engineering for Microservices
  • Shift-Right Testing: Smart Automation Through AI and Observability
  • Cost-Aware Resilience: Implementing Chaos Engineering Without Breaking the Budget

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!