Home of The JavaSpecialists' Newsletter

101bCausing Deadlocks in Swing Code (Follow-up)

Posted: 2005-01-19Category: GUIJava Version: 1.5+Dr. Heinz M. Kabutz
 

Abstract: 

 

It is very dangerous sending out code samples that one does not understand fully. So it was with the last newsletter. Fortunately Aleksey Gureev pointed out to me that the main problem was not with the Swing event thread, but rather with calling a static method from another thread before the static initializer block has completed. For example, the following code displays the same problem:

public class StrangeProblem2  {
  static {
    new StrangeProblem2();
  }

  private static void staticMethod() {
    System.out.println("This is never reached");
  }

  private StrangeProblem2() {
     Thread t = new Thread() {
      public void run() {
        System.out.println("We will now call the static method...");
        staticMethod();
        System.out.println("Static method was called.");
      }
    };
    t.start();

    try {
      t.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
  }
}
  

Kind regards

Heinz

 

Related Articles

Browse the Newsletter Archive

About the Author

demo

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

Nobody ever wants to call a Java performance consultant, but with first-hand experience repairing and improving commercial Java applications - JavaSpecialists are a good place to start...

Threading Emergency?

If your system is down, we will review it for 15 minutes and give you our findings for just 1 € without any obligation.