8 Replies Latest reply: Oct 9, 2006 9:06 AM by 791266 RSS

    Deadlock -> can some one explain pls?

    807607
      public class Lazy {
           private static boolean initialized = false;
           static {
                Thread t = new Thread(new Runnable() {
                     public void run() {
                          initialized = true; // Deadlocks here 
                     }
                });
      
                t.start();
                try {
                     t.join();
                } catch(InterruptedException e) {
                     throw new AssertionError(e);
                }
           }
      
           public static void main(String[] args) {
                System.out.println(initialized);
           }
      }
      The above given program deadlocks at the point specified in comment. Can someone explain the reason for this? Thanks in advance
        • 1. Re: Deadlock -> can some one explain pls?
          791266
          I really doubt that. I can't see any synchronized code.

          Kaj
          • 2. Re: Deadlock -> can some one explain pls?
            791266
            Try to execute this code and you'll see that it isn't a deadlock, but yes the application freezes.
            public class Lazy {
                 private static boolean initialized = false;
                 static {
                      Thread t = new Thread(new Runnable() {
                           public void run() {
                                System.out.println("Pre run");
                                initialized = true; // Deadlocks here 
                                System.out.println("Run done");
                           }
                      });
             
                      t.start();
                      System.out.println("Before join ");
                      try {
                           Thread.sleep(2000);
                      } catch (InterruptedException e) {
                           // TODO Auto-generated catch block
                           e.printStackTrace();
                      }
                      System.out.println("Done");
                 }
             
                 public static void main(String[] args) {
                      System.out.println(initialized);
                 }
            }
            Kaj
            • 3. Re: Deadlock -> can some one explain pls?
              807607
              Hi Kaj,
              This is what I am not able to understand. It freezes on the line where the variable is initialized. I could not think of any reason for this. I would like to know why the program hangs on that particular point.
              • 4. Re: Deadlock -> can some one explain pls?
                791266
                It freezes as long as the thread used by the class loader is executing the block. I don't know the reason to that but that's what it looks like.

                Kaj
                • 5. Re: Deadlock -> can some one explain pls?
                  mlk
                  My guess would be the VM has a lock on the Class until it has been completely loaded, which does make sense, handing it out before it is ready to be used would mean it would be in a unknown state.
                  • 6. Re: Deadlock -> can some one explain pls?
                    791266
                    My guess would be the VM has a lock on the Class
                    until it has been completely loaded, which does make
                    sense, handing it out before it is ready to be used
                    would mean it would be in a unknown state.
                    Sounds reasonable. But I do also expect some voodoo magic to be involved since the posted code isn't synchronized.
                    • 7. Re: Deadlock -> can some one explain pls?
                      mlk
                      Sounds reasonable. But I do also expect some voodoo
                      magic to be involved since the posted code isn't
                      synchronized.
                      If I understand the few lines of the VM Spec correctly, the above would lead to:
                      public class Lazy {
                           private static boolean initialized = false;
                           static {
                                Thread t = new Thread(new Runnable() {
                                     public void run() {
                                                      // <VM>
                                                      if( !Lazy.class.ready() ) {
                                                           synchronize( Lazy.class ) { 
                                                                   // Do Lazy init
                                                           }
                                                      // </VM>
                                          initialized = true; // Deadlocks here 
                                     }
                                });
                       
                                t.start();
                                try {
                                     t.join();
                                } catch(InterruptedException e) {
                                     throw new AssertionError(e);
                                }
                           }
                       
                           public static void main(String[] args) {
                                // <VM>
                                      if( !Lazy.class.ready() ) {
                                     synchronize( Lazy.class ) { 
                                                Do Lazy init
                                     }
                                }
                                // </VM>
                                System.out.println(initialized);
                           }
                      }
                      VM Spec (squiggly thing)2.17.5 Detailed Initialization Procedure

                      Message was edited by:
                      mlk
                      • 8. Re: Deadlock -> can some one explain pls?
                        791266
                        If I understand the few lines of the VM Spec
                        correctly, the above would lead to:
                        Sweeeeeet.