Tech Corner

The Basics of Threads and Thread Dumps

Shripati Bhat
.
Sep 13
.
3-4 mins

Table of contents

Automate your business at $5/day with Engati

REQUEST A DEMO
Switch to Engati: Smarter choice for WhatsApp Campaigns 🚀
TRY NOW
Threads and thread dumps

In java based applications, Threads form the basic part of every operation. In fact, threads are the very core in most of the processing units in the entire world of computers. As per theoretical definition, threads are lightweight processes that execute tasks assigned to them. But to explain in simpler terminologies, imagine you go to a bank and there is a queue of 10 people waiting (maintaining physical distancing :) ) to perform an action like open an account or withdraw or deposit or do other activities. Imagine there being 5 counters and 1 operator at each counter. This is what the bank counters would look like.

 

MAINTAIN SAFE DISTANCE

 

The first customer goes to the first counter, the second customer goes to the second counter, and so on till the fifth customer. The sixth customer however is still waiting in the queue. If any of the first 5 customers finish their activities, the counter gets vacated and the 6th customer can go to that counter. The 7th customer can go to the next vacated counter and so on.

 

Very similarly, threads are processed in a queue. The threads are picked up by the OS (Thread scheduling) per the priority and handed to the CPU for execution. As we are looking at an example of a Java application, it can have multiple threads in different states. Although each thread can only be in a single state at any given point.

 

Thread States

  • NEW:Indicates that a thread has just been created and hasn't been started yet (thread.start() method is not invoked yet). This indicates that the thread has just been assigned a task and the thread hasn't been submitted to the OS yet.
  • RUNNABLE:This indicates that the thread is executing in the JVM. This means that the thread is actually all set to execute instructions on a CPU that’s available. The Java application however doesn't know this detail of whether the thread is currently executing any instructions on the CPU or is it next in line. This time slicing or scheduling is left to the OS.
  • BLOCKED:Thread goes in a blocked state when it is waiting to acquire a lock on a semaphore (shared object/resource). This could happen when another thread has already acquired a lock and is doing some processing/waiting but hasn't released the monitor lock yet.
  • WAITING:This indicates that the thread is waiting indefinitely for another thread to perform a particular task. Generally, a thread on which we invoke t.wait() gets into this state. It continues in the state till another thread call t.notify() or t.notifyAll()
  • TIMED_WAITING:This indicates that the thread is waiting for another thread to perform a particular task for a specified waiting time. Generally, a thread on which we invoke t.wait(timeout) gets into this state.

  

Thread Dumps

If your JVM is utilizing CPU higher than usual or if you are working on a library that deals with a lot of thread operations or if your application is having deadlocks or even longer response time due to locks, thread dumps can come very handy for debugging. Also, part of scaling java based applications requires us to analyze what threads are doing at a given time under load conditions. Analyzing this can be done using a lot of tools like New Relic, Prometheus, etc. It could also need an analysis of a thread dump. You can get a thread dump of a java application using Java VisualVM or few other monitoring tools. But all the tools internally use the jstack command exposed by the JVM to take a thread dump. So effectively, you can take a thread dump from a terminal or command prompt.

 

Command to take a thread dump - jstack -l <pid> > dump1.txt

where pid is the process id of your java application (which can be got from the jps command)


Taking a single thread dump is not going to reveal a lot of information as it gives a snapshot or a point in time information of what all the threads in the JVM are doing. What we really need is at least 2 thread dumps taken over a brief duration. We can then compare the state of threads and check if they are stuck in a single operation for a prolonged period.

 

 

Analyzing Thread Dumps

 

A thread dump can be analyzed using multiple tools. There are many online thread dump analyzers like https://fastthread.io/ or https://spotify.github.io/threaddump-analyzer/ but what I personally prefer which also comes very handy is to analyze it in your IDE. A lot of IDEs like Intellij and Eclipse provide plugins that help analyze thread dumps very easily.


I'm going to focus on my favorite IDE Intellij. If you have upgraded to Intellij IDEA 2020 or above, you will see this option Analyze > Stack Trace or Thread Dump

 

 

 

When you paste the thread dump in this section, Intellij IDEA will analyze the thread dump and show you a consolidated summary of threads in various states. You could also include multiple such thread dumps and analyze them in one view.

 

 

Online thread dump analyzers even go a step ahead and show potential deadlocks or blocked calls. You can play around with this for some applications even in your local so that you guys can feel more comfortable with this when a situation asks for it. If you have questions as you try this, please do drop your comments below.


Shripati Bhat

Shripati is a Senior Director at Engati. He's a technical enthusiast, passionate about designing and building scalable software.

Shripati has a deeply ingrained customer-first ideology and is highly skilled in designing and developing Java/J2EE applications and BigData applications.

Close Icon
Request a Demo!
Get started on Engati with the help of a personalised demo.
This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
*only for sharing demo link on WhatsApp
Thanks for the information.
We will be shortly getting in touch with you.
Oops! something went wrong!
For any query reach out to us on contact@engati.com
Close Icon
Congratulations! Your demo is recorded.

Select an option on how Engati can help you.

I am looking for a conversational AI engagement solution for the web and other channels.

I would like for a conversational AI engagement solution for WhatsApp as the primary channel

I am an e-commerce store with Shopify. I am looking for a conversational AI engagement solution for my business

I am looking to partner with Engati to build conversational AI solutions for other businesses

continue
Finish
Close Icon
You're a step away from building your Al chatbot

How many customers do you expect to engage in a month?

Less Than 2000

2000-5000

More than 5000

Finish
Close Icon
Thanks for the information.

We will be shortly getting in touch with you.

Close Icon

Contact Us

Please fill in your details and we will contact you shortly.

This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
Thanks for the information.
We will be shortly getting in touch with you.
Oops! Looks like there is a problem.
Never mind, drop us a mail at contact@engati.com