Does the method.invoke function create another thread of execution? Because when I used it to call a GUI application's main method, the inv开发者_运维问答oke method returned immediately and the program and the GUI application were running at the same time.
No.
The behaviour you observe is caused by the fact that the GUI library starts an Event Dispatch Thread when initialized, and all following GUI-related work is performed by that thread.
EDIT Here is a simple Swing GUI application:
public class Test {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Hello, world!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println("Main method returns");
}
}
If you run it, you will see that all these methods are non-blocking, therefore main
method returns immediately after GUI is set up, and further work is performed by Event Dispatch Thread.
No. Method.invoke is working just like a normal method. If it is not you expect, maybe it throws Exception. Did you wrap the invocation?
Also, a snippet will help a lot
精彩评论