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

Deadlock while loading classes

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • 9
    • 9
    • hotspot
    • None

      The following example expects to be able to call code in the class from the static initializer. It's not clear to me (or anyone I've asked) if this is supposed to work or not. The result if running the example below is at least that the main thread hangs.

      public class CL implements Runnable {
        private static final Object mutex = new Object();
        private static boolean started = false;

        static {
          Thread t = new Thread(new CL());
          t.start();
          synchronized (mutex) {
            try {
              while (!started) {
                mutex.wait();
              }
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        }

        @Override
        public void run() {
          started = true;
          synchronized (mutex) {
            mutex.notify();
          }
        }

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

            Unassigned Unassigned
            jwilhelm Jesper Wilhelmsson
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: