从ShutdownHook调用非静态方法 [英] Call a non-static method from ShutdownHook

查看:53
本文介绍了从ShutdownHook调用非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从ShutdownHook中将该方法称为非静态方法 removeLocksOnExit().该方法在与main相同的类中声明.

主要,我有以下代码:

 //最终线程mainThread = Thread.currentThread();Runtime.getRuntime().addShutdownHook(new Thread(){公共无效run(){//删除锁定文件...removeLocksOnExit();}}); 

removeLocks()不能被静态删除,因此我无法直接从新线程调用它.

主类包含一个关闭窗口的动作列表器,该动作列表器也调用该方法.这是代码的一部分:

  addWindowListener(new WindowAdapter(){@Overridepublic void windowClosing(WindowEvent e){如果(exit()== 0){removeLocksOnExit();log.info("END");System.exit(0);}}}); 

也许我可以从addShutdownHook中触发Windows关闭事件.这将调用该函数给我.(或创建一个特殊事件以进行缓存...

可行吗?如果是,您将如何触发该事件?

在这里有一个更好的理解是我班级的结构:

 公共类MyTool扩展JFrame {removeLocksOnExit(){.......}公共静态void main(String [] args){............//最终线程mainThread = Thread.currentThread();Runtime.getRuntime().addShutdownHook(new Thread(){公共无效run(){//删除锁定文件...removeLocksOnExit();}});}final MyTool inst =新的MyTool(args);MyTool(String [] args){超级(CustomPathModel.MyTITLE);setResizable(false);//FIXME找到一种自动调整内部xml编辑器大小的方法addWindowListener(new WindowAdapter(){@Overridepublic void windowClosing(WindowEvent e){如果(exit()== 0){removeLocksOnExit();log.info("END");System.exit(0);}}});setLookFeel();initAdminStatus();initGUI();addToolActionListener(this);}} 

解决方案

您可以在构造函数中添加关闭窗口事件.还要在其中添加关闭钩子.

当心,但是,可以从不同的线程两次调用removeLocks方法.请注意它会自行清除,并且是线程安全的.还应确保在创建窗口侦听器或关闭挂钩之前,在 之前已准备好运行removeLocks方法(即,其所需的所有变量均已初始化).

I need to call the method a non static method removeLocksOnExit() from a ShutdownHook. the method is declared in te same class where the main is.

In the main i have the following code:

//final Thread mainThread = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        // remove lock file...
        removeLocksOnExit();
    }
});

The removeLocks() cannot be decared static and therefore i canot call it directly from the new thread.

The main class contains However an action listners over the windows closing which calls the method as well. Here is an extract of the code:

addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        if (exit() == 0) {
            removeLocksOnExit();
            log.info("END");

            System.exit(0);
        }
    }
});

Perhaps i could then from the addShutdownHook fire the windows closing event. and this one will call the function fore me. ( Or create a special event for the purpose to be cached...

Is it feasible?? If yes how would you fire the event??

to have a better understanding here is the structure of my class:

public class MyTool extends JFrame{

removeLocksOnExit(){
....
...
}

 public static void main(String[] args) {
...............
.........
    //final Thread mainThread = Thread.currentThread();
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            // remove lock file...
            removeLocksOnExit();
        }
    });
}

final MyTool inst = new MyTool(args);

MyTool(String[] args) {
        super(CustomPathModel.MyTITLE);
        setResizable(false); // FIXME find a way to auto size inner xml-editor
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                if (exit() == 0) {
                    removeLocksOnExit();
                    log.info("END");

                    System.exit(0);
                }
            }
        });

        setLookFeel();
        initAdminStatus();
        initGUI();
        addToolActionListener(this);
    }
}

解决方案

You add your window-closing event in the constructor. Add the shutdown hook there as well.

Beware, however, that the removeLocks method might be called twice, from different threads. Take care that it cleans up after itself, and that it is threadsafe. Also make certain that the removeLocks method is ready to run (ie, all variables that it needs have been initialized) before you create either the window listener or the shutdown hook.

这篇关于从ShutdownHook调用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆