1

我想弄清楚的是,当程序终止时,如何显示来自其他类对象的数据成员的值。我的意思是,使用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类对象访问对象数据成员?

4

0 回答 0