Java

Thread Dump Analysis

In this article we are going to demonstrate how to identify issues with threads and analyze the thread dump by creating a deadlock situation.

Problem

Create a deadlock between two threads.

Java Code

package com.threaddump.test;

import java.awt.List;
import java.util.Map;

public class ThreadDumpTest {

	public static void main(String args[]) {
		
		Runnable task1 = () -> {
			synchronized (List.class) {
				synchronized (Map.class) {
					System.out.println("Thread 1");
				}
			}
		};

		Runnable task2 = () -> {
			synchronized (Map.class) {
				synchronized (List.class) {
					System.out.println("Thread 2");
				}
			}
		};

		new Thread(task1).start();
		new Thread(task2).start();
	}
}

Analysis using jconsole

  • Open Jconsole and connect to the JVM where deadlock has occurred.
  • Open Thread tab and identify the problem

Thread0 is blocked on a lock which is acquired by Thread1 & Thread1 is blocked on a lock which is acquired by Thread0.

  • Analysis by taking thread dump and jtda
  • Take thread dump

C:\Users\abhishek.tiwari>jstack -l 24304 >> threadDump.logs

  • Download jtda.jar

C:\Users\abhishek.tiwari>java -jar C:\Software\jtda.jar

  • Copy paste threadDump.logs on jtda UI and click analyze.

Conclusion#

Check blocked threads as you can see Thread 1 is waiting for lock on java.awt.List which is taken up by Thread 2 and Thread 2 is waiting for lock on java.util.Map which is taken up by Thread 1.

Leave a Reply

Your email address will not be published. Required fields are marked *