Within the sequence of chaos engineering articles, now we have been studying to simulate numerous efficiency issues. On this submit, let’s talk about how one can make threads go right into a BLOCKED state.
Here’s a pattern program from the open-source BuggyApp utility, which might make threads go right into a BLOCKED state. A thread will enter right into a BLOCKED state when it couldn’t purchase a lock on an object as a result of one other thread already holds the lock on the identical object and doesn’t launch it. Assessment this system fastidiously.
public class BlockedAppDemo
public static void begin()
for (int counter = 0; counter < 10; ++counter)
// Launch 10 threads.
public class AppThread extends Thread
public void run()
public class AppObject
public static synchronized void getSomething()
Thread.sleep(10 * 60 * 1000);
catch (Exception e)
The pattern program comprises the ‘
BlockedAppDemo’ class. This class has
begin() technique. On this technique,’ 10 new threads are created. In AppThread class there’s a
run() technique that invokes
getSomething() technique on the AppObject. On this
getSomething() technique, a thread is put to steady sleep, i.e., the thread is repeatedly sleeping for 10 minutes time and again. However for those who discover,
getSomething() technique is a synchronized technique. Synchronized strategies might be executed by just one thread at a time. If every other thread tries to execute the
getSomething() technique whereas the earlier thread continues to be engaged on the strategy, then the brand new thread will likely be put within the BLOCKED state.
On this case, 10 threads are launched to execute
getSomething() technique. However nevertheless, just one thread will purchase a lock and execute this technique, the remaining 9 threads will likely be put within the BLOCKED state.
NOTE: If threads are BLOCKED for a chronic interval, the appliance could turn out to be unresponsive.
Tips on how to Diagnose ‘Blocked Thread’?
You possibly can diagnose Blocked Thread both by way of a handbook or automated strategy.
Within the handbook strategy, you want to seize thread dumps as step one. A thread dump exhibits all of the threads which might be in reminiscence and their code execution path. You possibly can seize thread dump utilizing one of many 8 choices talked about right here. However an necessary standards is: You have to seize thread dump proper when the issue is occurring. As soon as the thread dump is captured, you want to import the thread dump out of your manufacturing servers to your native machine. You need to use thread dump evaluation instruments like fastThread, samurai to investigate the thread dumps out of your native machine.
You need to use root trigger evaluation instruments like yCrash, which mechanically captures application-level knowledge (thread dump, heap dump, Rubbish Assortment log) and system-level knowledge (netstat, vmstat, iostat, high, high -H, dmesg,…). In addition to capturing the info mechanically, it marries application-level knowledge and system-level knowledge and generates an prompt root trigger evaluation report. Under is the report generated by the yCrash software when the above pattern program is executed:
Fig: yCrash reporting the road of code through which 9 threads are within the blocked state.
You possibly can discover the yCrash software reporting 9 threads are within the BLOCKED state and it’s additionally stating the stack hint through which they’re caught. From the stacktrace you’ll be able to observe that thread is caught on ‘
Fig: yCrash transitive graph exhibiting BLOCKED threads.
yCrash prints a transitive dependency graph that exhibits what thread is obstructing what threads. On this transitive graph, you’ll be able to see ‘Thread-19’ blocking 9 different threads. In case you click on on the thread names within the graph, you’ll be able to see the stack hint of that individual thread. While you click on on ‘Thread-19’, you’ll discover that the thread is caught on the sleep() technique in java.lang.Thread. Stack hint of ‘Thread-19’ may also level out that earlier than getting caught, this thread has obtained 1 lock, and resulting from which 9 threads are put within the BLOCKED state.