Java Specialists' Java Training Europehome of the java specialists' newsletter

The Java Specialists' Newsletter
Issue 101b2005-01-19 Category: GUI Java version: Sun JDK 1.5.0_01

GitHub Subscribe Free RSS Feed

Causing Deadlocks in Swing Code (Follow-up)

by Dr. Heinz M. Kabutz

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

GUI Articles Related Java Course

Extreme Java - Concurrency and Performance for Java 8
Extreme Java - Advanced Topics for Java 8
Design Patterns
In-House Courses

© 2010-2016 Heinz Kabutz - All Rights Reserved Sitemap
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. JavaSpecialists.eu is not connected to Oracle, Inc. and is not sponsored by Oracle, Inc.