Wednesday, May 8, 2024
HomeJavaSimulating & troubleshooting BLOCKED threads in Kotlin - Java Code Geeks

Simulating & troubleshooting BLOCKED threads in Kotlin – Java Code Geeks


On this sequence of simulating and troubleshooting efficiency issues in Kotlin, let’s focus on the way to make threads go into 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.

Kotlin blocked thread Program

Here’s a pattern program, which might make threads go into BLOCKED state.

bundle com.buggyapp
   class BlockedApp {
      enjoyable begin() {
         println("BlockedApp:began")
         for (counter in 0..9) { // Launch 10 threads.
            AppThread().begin()
         }
      }
   }

   class AppThread : Thread() {
      override enjoyable run() {
         AppObject.one thing
      }
   }

   object AppObject {
      @get:Synchronized 
      val one thing: Unit
         get() {
            whereas (true) {
               attempt {
                  Thread.sleep(6000000.toLong())
               } catch (e: Exception) {
               }
            }
         }
   }

   enjoyable predominant() {
      println(BlockedApp().begin())
   }

Pattern program accommodates the ‘BlockedApp’ class. This class has a begin() methodology. On this methodology, 10 new threads are created. Within the AppThread class there’s a run() methodology that invokes getSomething() methodology on the AppObject. On this getSomething() methodology, thread is put to steady sleep i.e. thread is repeatedly sleeping for 10 minutes repeatedly. However in the event you discover, the getSomething() methodology is a synchronized methodology. Synchronized strategies may be executed by just one thread at a time. If every other thread tries to execute the getSomething() methodology whereas the earlier thread continues to be engaged on it, then the brand new thread will likely be put within the BLOCKED state. 

On this case, 10 threads are launched to execute the getSomething() methodology. Nonetheless, just one thread will purchase lock and execute this methodology, remaining 9 threads will likely be put in BLOCKED state.

NOTE: If threads are in BLOCKED state for a protracted interval, then software might turn out to be unresponsive.

Easy methods to diagnose BLOCKED threads? 

You may diagnose BLOCKED threads both by a guide or automated method.

Handbook method

Within the guide method, it is advisable to seize thread dumps as step one. A thread dump reveals all of the threads which are in reminiscence and their code execution path. You may seize thread dump utilizing one in every of the 8 choices talked about right here. However an essential standards is: You might want to seize thread dump proper when the issue is occurring (which is likely to be tough to do). As soon as the thread dump is captured, it is advisable to manually import the thread dump out of your manufacturing servers to your native machine and analyze it utilizing thread dump evaluation instruments like fastThread, samurai.

Automated method

Alternatively, you may also use yCrash open supply script, which might seize 360-degree knowledge (GC log, 3 snapshots of thread dump, heap dump, netstat, iostat, vmstat, prime, prime -H,…) proper when the issue surfaces within the software stack and analyze them immediately to generate root trigger evaluation report. 

We used the automated method. Under is the foundation trigger evaluation report generated by the yCrash device highlighting the supply of the issue. 

Fig:  yCrash reporting transitive dependency graph of 9 BLOCKED threads

yCrash prints a transitive dependency graph that reveals which threads are getting BLOCKED and who is obstructing them. On this transitive graph, you possibly can see ‘Thread-0’ blocking 9 different threads. In case you click on on the thread names within the graph, you possibly can see the stack hint of that exact thread.

Fig:  yCrash reporting the stack hint of 9 threads which are in BLOCKED state

Right here is the screenshot that reveals the stack hint of the 9 threads that are in BLOCKED state and it’s additionally declaring the stack hint wherein they’re caught. From the stacktrace you possibly can observe that thread is caught on ‘com.buggyapp.blockedapp.AppObject#getSomething()’ methodology.

Geared up with this data, one can simply establish the foundation reason for the BLOCKED state threads. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments