Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Chaos Engineering – Blocked Threads

  1. #1
    Member
    Join Date
    Nov 2017
    Location
    USA
    Posts
    137
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Chaos Engineering – Blocked Threads

    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 BLOCKED state.

    Sample Program
    Here is a sample program from open source BuggyApp application, which would make threads go into 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) {}
    	}
             }
    }

    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, thread is put to continuous sleep i.e. 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 lock and execute this method, remaining 9 threads will be put in BLOCKED state.

    NOTE: If threads are in BLOCKED for a prolonged period, then 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:



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

    You can notice yCrash tool reporting 9 threads are in 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.



    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 thread is stuck on sleep() method in java.lang.Thread. Stack trace of ‘Thread-19’ will also points out that before getting stuck, this thread has obtained 1 lock, and due to which 9 threads are put in BLOCKED state.

  2. #2
    Junior Member
    Join Date
    Oct 2021
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Chaos Engineering – Blocked Threads

    Keep sharing that useful information with us.

Similar Threads

  1. Chaos Engineering – Stackoverflow Error
    By Ram Lakshmanan in forum Java Networking Tutorials
    Replies: 0
    Last Post: May 16th, 2021, 11:03 PM
  2. Chaos Engineering – Thread Leak
    By Ram Lakshmanan in forum Java Programming Tutorials
    Replies: 0
    Last Post: April 28th, 2021, 11:01 PM
  3. Chaos Engineering – Deadlock
    By Ram Lakshmanan in forum JDBC and Database Tutorials
    Replies: 0
    Last Post: March 27th, 2021, 05:13 AM
  4. Chaos Engineering – Simulating OutOfMemoryError
    By Ram Lakshmanan in forum Java SE API Tutorials
    Replies: 0
    Last Post: March 7th, 2021, 11:43 PM
  5. Chaos Problems.
    By zmontoya94 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 6th, 2012, 10:40 AM