1

What am trying to figure out is, how to display the values of the datamembers from other class objects, when the program terminates. I mean, using the ShutdownHook, which will be accessed when pressing the Ctrl+C to terminate the program.

The following is a sample code:

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);

    }
}

How will I be able to pass the data from the mtc[] objects to Stats class object or how can I access the mtc[] objects datamembers from Stats class object?

Vpp Man
  • 2,384
  • 8
  • 43
  • 74

0 Answers0