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
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)
We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.