简体   繁体   English

如何等待反射线程完成

[英]How to wait for a reflection thread to finish

I have an application class which allows the user to download a jar file, this jar file is then accessed using reflection. 我有一个应用程序类,该类允许用户下载一个jar文件,然后使用反射访问该jar文件。

public void install() {
    File app = new File("C:/Temp/" + this.name + ".jar");
    if(!app.exists())
        download();

    URLClassLoader appLoader;
    Class<?> appBuilder = null;

    try {
        appLoader = URLClassLoader.newInstance(new URL[] { new URL("C:/Temp/" + this.name + ".jar") });
        appBuilder = appLoader.loadClass("iezon.app.App");
    } catch (MalformedURLException | ClassNotFoundException e) {
        WebSocket.addError(e);
    }

    this.application = appBuilder;
}

private void download() {
    try {
        FileUtils.copyURLToFile(new URL(this.downloadUrl), new File("C:/Temp/" + this.name + ".jar"));
    } catch (IOException e) {
        WebSocket.addError(e);
    }
}

Here, I am creating a new instance of the jar file: 在这里,我正在创建jar文件的新实例:

public void start() {
    try {
        App.createWindow(this.application.getClass());
    } catch (InstantiationException | IllegalAccessException e) {
        WebSocket.addError(e);
    }
}

Window is custom extension of a JFrame that is used as a base template for GUI design. Window是JFrame的自定义扩展,用作GUI设计的基本模板。

public static int createWindow(Class<?> window) throws InstantiationException, IllegalAccessException {
    factory.add((Window) window.newInstance());
    factory.get(factory.size() - 1).windowId = factory.size() - 1;
    factory.get(factory.size() - 1).run();
    return factory.size() - 1;
}

Since the jar file, once instanced, cannot access this code to load the home screen window on exit, I was wondering how I can wait for the jar file instance to be closed and then relaunch the home screen: 由于jar文件一旦实例化,就无法访​​问此代码以在退出时加载主屏幕窗口,我想知道如何等待jar文件实例关闭然后重新启动主屏幕:

ie in suedo code: 即在suedo代码中:

when (create App.createWindow(this.application.getClass()))
dies
create App.createWindow(HomeScreen.class)

Is there a way I can use wait() and notify() methods? 有没有一种方法可以使用wait()notify()方法? Or possibly add a listener when instancing the Class like factory.get(factory.size() - 1).addSomeExitListener.....() ? 或者在实例化类(例如factory.get(factory.size() - 1).addSomeExitListener.....()时添加侦听器?

You can forget about the reflection part, it's not really relevant since the same would be true if you were given a regularly constructed instance. 您可能会忘记反射部分,它并不是真正相关的,因为如果为您提供一个定期构造的实例,那也是正确的。

You'll need to design your Window class in a way that it works as desired, eg let it callback you when it's done. 您需要以一种可以按需工作的方式设计Window类,例如,完成后让其回调您。 Since it is a JFrame you could also use it's window close listener like: 由于它是一个JFrame您也可以使用它的窗口关闭侦听器,例如:

public static int createWindow(Class<?> window) throws InstantiationException, IllegalAccessException {
    Window instance = (Window) window.newInstance();
    factory.add(instance);
    instance.windowId = factory.size() - 1;
    if (window != HomeScreen.class) {
        // instance.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // ?
        instance.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                App.createWindow(HomeScreen.class);
            }
        });
    }
    instance.run();
    return factory.size() - 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM