开发者

Does Method.invoke create another thread of execution?

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜