Lưu ý câu trả lời này chỉ và khoảng Làm thế nào thực sự là chủ đề ban đầu làm việc với các phương pháp tĩnh trong Java8 là, nó không tính đến các hành vi trong thời gian chạy
thử nghiệm đơn giản bằng cách sử dụng invokeLater khá agr ee với ý kiến ở đây, để hiểu rõ hơn
có vẻ như là lỗi hoặc tính năng trong JDK8 và Swing API (?? giống như lỗi hoặc tính năng về loại bỏ tất cả các phương pháp Safe Chủ đề trong JDK7)
martin đã viết - Tôi biết những gì tôi đang làm - đôi khi invokeAndWait là cần thiết và cho đến khi điều này tôi chưa bao giờ có vấn đề với nó. - chưa bao giờ thấy tình huống này, không bao giờ cần thiết để sử dụng invokeAndWait trong Todays Java phiên bản Java 1.6 và không bao giờ phiên bản
AWT tổ chức sự kiện hàng đợi cần initializations của AWT JComponents/Swing
API cho AWT/Swing GUI doesn 'bảo đảm trật tự t sự kiện
- bằng cách sử dụng tiêu chuẩn
invokeLater
là đầu ra không có thành phần (J), mọi thứ đều là o.k., tất cả ba luồng đều kết thúc thành công
.
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)
- bằng cách sử dụng tiêu chuẩn
invokeLater
và JFrame, mọi thứ đều ok, cả ba chủ đề kết thúc với JFrame trên màn hình, thành công (điều này là chính xác đầu ra, Đang chờ đó)
.
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
- bằng cách sử dụng tiêu chuẩn
invokeAndWait
không (J) Linh kiện, không bao giờ kết thúc từ lambda expresion, nó phải bị giết từ 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
- bằng cách sử dụng
invokeAndWait
và JFrame
không bao giờ cho thấy JFrame khởi tạo từ lambda expresion, nó phải bị giết từ 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)
.
từ mã
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();
}
}
và
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);
}
}
.
chỉ sử dụng invokeAndWait nếu bạn THỰC HIỆN biết bạn đang làm gì. –
Tôi biết những gì tôi đang làm - đôi khi invokeAndWait là cần thiết và cho đến khi điều này tôi chưa bao giờ có vấn đề với nó. – Martin
Vì vậy, nó chỉ tay trên lambda? Nếu bạn thay đổi thứ tự của cuộc gọi, nó vẫn chỉ treo trên lambda? –