I have a class like this -
class MainClass {
X x; Y y; Z z;
public MainClass () {
// initialise x, y, z here
}
I want to add a shutdownhook, and cleanup the resources in x,y,z. For that I created a class Cleaner (extends
Thread).
Now my question is this. is the following technique valid -
I add a method called clean() in MainClass, that cleans all thats needed.
I write my Cleaner class like this-
class Cleaner extends Thread {
private MainClass mainc;
public Cleaner(MainClass mainc) {
this.mainc = mainc;
}
public void run () {
mainc.clean();
}
I modify the MainClass like this -
class MainClass {
X x; Y y; Z z;
Cleaner c;
public MainClass () {
// initialise x, y, z here.
c = new Cleaner(this);
//get a runtime object.
runtime.addShutdownHook(c);
}
Now my doubt is, the 'this' object is being passed to cleaner's constructor. but this is happening when the mainclass itself is not fully initialised (that is, while its still in its constructor). wat will b the behavior of the Cleaner now? will it be consistent, and will it clean the resources properly?