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) 2015, HerongYang.com, All Rights Reserved.
 */
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.8.0\bin\javac SimpleDeadLock.java

C:\herong>\Progra~1\java\jdk1.8.0\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.

Last update: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

JVM Troubleshooting Tools

 JVM Troubleshooting Tools in JDK 1.5

 'jinfo' - VM Option Value Checker

 Changing HotSpot VM Option using 'jinfo'

 'jstack' - Stack Tracer to Generate Thread Dump

 What Is a Thread Dump?

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

 jvisualvm (Java VisualVM) - JVM Visual Tool

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 PDF Printing Version