Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.11

Java Thread Deadlock Demo Program

This section provides a tutorial example of simple deadlock demo program, SimpleDeadLock.java, with 2 threads waiting for each other.

In the previous section, "jstack" was used to print stack traces of all running threads of a given JVM process. But "jstack" can also be used to detect deadlocks inside a given JVM process. The tutorial example below shows you how "jstack" prints deadlock information:

1. Copy and save this Java program,

/**
 * SimpleDeadLock.java
 * Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
 */
import java.util.*;
public class SimpleDeadLock extends Thread {
   public static Object l1 = new Object();
   public static Object l2 = new Object();
   private int index;
   public static void main(String[] a) {
      Thread t1 = new Thread1();
      Thread t2 = new Thread2();
      t1.start();
      t2.start();
   }
   private static class Thread1 extends Thread {
      public void run() {
         synchronized (l1) {
            System.out.println("Thread 1: Holding lock 1...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 1: Waiting for lock 2...");
            synchronized (l2) {
               System.out.println("Thread 2: Holding lock 1 & 2...");
            }
         }
      }
   }
   private static class Thread2 extends Thread {
      public void run() {
         synchronized (l2) {
            System.out.println("Thread 2: Holding lock 2...");
            try { Thread.sleep(10); }
            catch (InterruptedException e) {}
            System.out.println("Thread 2: Waiting for lock 1...");
            synchronized (l1) {
               System.out.println("Thread 2: Holding lock 2 & 1...");
            }
         }
      }
   }
}

2. Compile and run SimpleDeadLock.java. A deadlock will be created immediately between two running threads:

C:\herong>\Progra~1\java\jdk1.6.0_02\bin\javac SimpleDeadLock.java

C:\herong>\Progra~1\java\jdk1.6.0_02\bin\java SimpleDeadLock

Thread 1: Holding lock 1...
Thread 2: Holding lock 2...
Thread 2: Waiting for lock 1...
Thread 1: Waiting for lock 2...

This deadlock is expected - Thread 1 is holding lock 1 and waiting for lock 2, while thread 2 is holding lock 2 and waiting for lock 1.

See the next section on how to use "jstack" to print the deadlock information and stack traces of 2 related threads.

Sections in This Chapter

JVM Troubleshooting Tools in JDK 1.5

'jinfo' - VM Option Value Checker

Changing HotSpot VM Option using 'jinfo'

'jstack' - Stack Tracer of JVM Threads

Java Thread Deadlock Demo Program

Detecting Java Thread Deadlocks with 'jstack'

'jmap' - JVM Heap Dump Tool

Printing Histogram of Java Object Heap

Generating Heap Dump File with 'jmap'

'jhat' - Java Heap Analysis Tool

Starting 'jhat' Web Server on a Heap Dump File

Listing Instance Counts of All Classes

Browsing Object Instance Values

Object Query Language (OQL)

Searching for Instances with OQL Statements

Dr. Herong Yang, updated in 2008
Java Thread Deadlock Demo Program