Note this answer is just and about How really the Initial Thread works with static methods in Java8, it
doesn't account for the behavior at runtime
simple testing by using invokeLater quite agree with comments here, for better understanding
seems like as bug or feature in JDK8 and Swing APIs (??the same as bug or feature about removing all Thread Safe methods in JDK7 )
martin wrote - I know what I'm doing - sometimes invokeAndWait is necessary and until this I've never had issues with it. - never seen this situations, never needed to use invokeAndWait in todays Java versions Java 1.6 and never versions
AWT Event queue required initializations of AWT/Swing JComponents
APIs for AWT/Swing GUI doesn't guarantee ordering of events
- by using standard
invokeLater
is output without (J)Components, everything is o.k., all three threads ends with success
.
initializer start
- anonymous inner-class: Print.print
- method ref: Print.print
* Print.print called from - anonymous inner-class
- lambda: Print.print
** ** Print.print called from - method ref
- initializer end
* Print.print called from - lambda
BUILD SUCCESSFUL (total time: 1 second)
- by using standard
invokeLater
and JFrame , everything is o.k., all three threads ends with JFrame on the screen, success (this is correct output, awating that)
.
initializer start
- anonymous inner-class: Print.print
- method ref: Print.print
- lambda: Print.print
- initializer end
* Print.print called from - anonymous inner-class
** ** Print.print called from - method ref
* Print.print called from - lambda
- by using standard
invokeAndWait
without (J)Components, never ends from lambda expresion, it must be killed from IDE
.
initializer start
- anonymous inner-class: Print.print
* Print.print called from - anonymous inner-class
- method ref: Print.print
** ** Print.print called from - method ref
- lambda: Print.print
- by using
invokeAndWait
and JFrame
never shows jframe initialized from lambda expresion, it must be killed from IDE
.
run:
initializer start
- anonymous inner-class: Print.print
* Print.print called from - anonymous inner-class
- method ref: Print.print
** ** Print.print called from - method ref
- lambda: Print.print
BUILD STOPPED (total time: 3 minutes 40 seconds)
.
from code
public class Test {
// A normal (non-static) initializer does not have the problem
static {
try {
System.out.println("initializer start");
// --- Works
System.out.println("\n - anonymous inner-class: Print.print");
EventQueue.invokeLater/*EventQueue.invokeAndWait*/(new Runnable() {
@Override
public void run() {
Print.print("anonymous inner-class");
}
});
// --- Works
System.out.println("\n - method ref: Print.print");
EventQueue.invokeLater/*EventQueue.invokeAndWait*/(Print::print);
// --- Hangs forever
System.out.println("\n - lambda: Print.print");
EventQueue.invokeLater/*EventQueue.invokeAndWait*/(() -> Print.print("lambda"));
System.out.println("\n - initializer end");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Test();
}
}
and
import javax.swing.JFrame;
public class Print {
public static final void print() {
/*
JFrame frame = new JFrame();
frame.setTitle("called from - method ref");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLocationByPlatform(true);
frame.setVisible(true);*/
System.out.println(" ** ** Print.print called from - method ref");
}
public static final void print(String str) {
/*
JFrame frame = new JFrame();
frame.setTitle("called from - " + str);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLocationByPlatform(true);
frame.setVisible(true);*/
System.out.println(" * Print.print called from - " + str);
}
}
.