将值传递给ShutdownHook或从ShutdownHook访问其他对象的值

时间:2014-04-18 11:20:36

标签: java shutdown-hook

试图弄清楚的是,当程序终止时,如何从其他类对象显示数据库的值。我的意思是,使用ShutdownHook,在按Ctrl+C以终止程序时将访问该文件。

以下是示例代码:

public class Test{
    public static void main(String[] args) {
        MyThreadClass mtc[] = new MyThreadClass[20]; // where, MyThreadClass extends Thread

        for(int i=0; i<20; i++)
        {
            mtc[i] = new MyThreadClass();
            mtc[i].start();     // here during the execution, some class members are being updated(say, increments the values or something else) and I want to show this updated values when the hook is being activated. I mean, when the program terminates.
        }


        // have created another class to show the stats (ie, after program termination)
        // so when program terminates, the run() method in the Stats class is being executed and it should access each elements of the mtc[] and display the value of some of the public data members
        Stats s = new Stats();  
        Runtime.getRuntime().addShutdownHook(s);

    }
}

我如何能够将mtc[]对象中的数据传递给Stats类对象,或者如何从mtc[]类对象访问Stats个对象数据库?

0 个答案:

没有答案