Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8185010

jstack should provide support for diagnosing deadlocks in class initialization

XMLWordPrintable

      See https://stackoverflow.com/questions/27549671/how-to-diagnose-or-detect-deadlocks-in-java-static-initializers

      public class StaticDeadlockExample implements Runnable
      {
          static
          {
              Thread thread = new Thread(
                      new StaticDeadlockExample(),
                      "StaticDeadlockExample child thread");
              thread.start();
              try {
                  thread.join();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }

          public static void main(String[] args)
          {
              System.out.println("in main");
          }

          public static void sayHello()
          {
              System.out.println("hello from thread " + Thread.currentThread().getName());
          }

          @Override
          public void run() {
              StaticDeadlockExample.sayHello();
          }
      }

      This program deadlocks. The stack trace at time of deadlock (from jstack) contains the following two deadlocked threads, but provides no clear indication of the deadlock. It lists one of the thread as RUNNABLE.

      "StaticDeadlockExample child thread" prio=6 tid=0x000000006c86a000 nid=0x4f54 in Object.wait() [0x000000006d38f000]
         java.lang.Thread.State: RUNNABLE
          at StaticDeadlockExample.run(StaticDeadlockExample.java:37)
          at java.lang.Thread.run(Thread.java:619)

         Locked ownable synchronizers:
          - None

      "main" prio=6 tid=0x00000000005db000 nid=0x2fbc in Object.wait() [0x000000000254e000]
         java.lang.Thread.State: WAITING (on object monitor)
          at java.lang.Object.wait(Native Method)
          - waiting on <0x000000004a6a7870> (a java.lang.Thread)
          at java.lang.Thread.join(Thread.java:1143)
          - locked <0x000000004a6a7870> (a java.lang.Thread)
          at java.lang.Thread.join(Thread.java:1196)
          at StaticDeadlockExample.<clinit>(StaticDeadlockExample.java:17)
          at java.lang.Class.forName0(Native Method)
          at java.lang.Class.forName(Class.java:169)
          at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)

         Locked ownable synchronizers:
          - None

            Unassigned Unassigned
            iklam Ioi Lam
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: