Technical Stuff

Start from basics

Thread Dump

Thread Dump
Thread Dump
5 (100%) 1 vote
  1. When there is an obstacle, or when a Java based Web application is running much slower than expected, we need to use thread dump. Here I will explain what threads are in Java, their types, how they are created, how to manage them, how you can dump threads from a running application.

    Java and Thread

    A web server uses tens to hundreds of threads to process a large number of concurrent users. If two or more threads utilize the same resources, a contention between the threads is inevitable, and sometimes deadlock occurs.

    Thread contention is a status in which one thread is waiting for a lock, held by another thread, to be lifted. Different threads frequently access shared resources on a web application. For example, to record a log, the thread trying to record the log must obtain a lock and access the shared resources.

    Deadlock is a special type of thread contention, in which two or more threads are waiting for the other threads to complete their tasks in order to complete their own tasks.

    Different issues can arise from thread contention. To analyze such issues, you need to use the thread dump. A thread dump will give you the information on the exact status of each thread.

    Background Information for Java Threads

    Thread Synchronization

    A thread can be processed with other threads at the same time. In order to ensure compatibility when multiple threads are trying to use shared resources, one thread at a time should be allowed to access the shared resources by using thread synchronization.

    Thread synchronization on Java can be done using monitor. Every Java object has a single monitor. The monitor can be owned by only one thread. For a thread to own a monitor that is owned by a different thread, it needs to wait in the wait queue until the other thread releases its monitor.

    Thread Status

    In order to analyze a thread dump, you need to know the status of threads. The status of threads are stated on java.lang.Thread.State.

    Thread lifecycle

    • NEW: The thread is created but has not been processed yet.
    • RUNNABLE: The thread is occupying the CPU and processing a task. (It may be in WAITINGstatus due to the OS’s resource distribution.)
    • BLOCKED: The thread is waiting for a different thread to release its lock in order to get the monitor lock.
    • WAITING: The thread is waiting by using a wait, join or park method.
    • TIMED_WAITING: The thread is waiting by using a sleep, wait, join or park method. (The difference from WAITING is that the maximum waiting time is specified by the method parameter, and WAITING can be relieved by time as well as external changes.)

    Thread Types

    Java threads can be divided into two:

    1. daemon threads;
    2. and non-daemon threads.

    Daemon threads stop working when there are no other non-daemon threads. Even if you do not create any threads, the Java application will create several threads by default. Most of them are daemon threads, mainly for processing tasks such as garbage collection or JMX.

    A thread running the ‘static void main(String[] args)’ method is created as a non-daemon thread, and when this thread stops working, all other daemon threads will stop as well. (The thread running this main method is called the VM thread in HotSpot VM.)

    Getting Thread Dump

    We can take thread dump using following command.

    Command  >> Kill -3 pid

    Happy Learning !!!

    If you have doubt or queries, you can definetely comment us or can mail us on [email protected]

    If you have any recommedation for future blog, You can email us on [email protected]

    Related Searches :

    1. Heap Dump
    2. Jboss Installation
    3. Deployment in Jboss

3 thoughts on “Thread Dump

  1. Hey Gaurai Vaidya,
    In unix or linux based OS, Kill command basically sends signal to a running process.
    Process run through a unique process ID known as PID.
    To send signal to a perticular process in linux
    Kill -SIGNAL ID PID

    Like that when you do kill -3 PID
    it simple instruct the process associated with the Id to dump its all thread information to a readable file which we term as threadump

Leave a Reply

%d bloggers like this: