Running on Java 22-ea+27-2262 (Preview)
Home of The JavaSpecialists' Newsletter

101bCausing Deadlocks in Swing Code (Follow-Up)

Author: Dr. Heinz M. KabutzDate: 2005-01-19Java Version: 1.2Category: GUI
 

Abstract: A follow-up to explain that the previous code was not the "refresh bug", but rather an issue with the static initializer starting a thread and that thread calling a static method.

 

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

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

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

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

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

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...