aspose file tools*
The moose likes Java in General and the fly likes Cleaning up with shutdownhook Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cleaning up with shutdownhook" Watch "Cleaning up with shutdownhook" New topic
Author

Cleaning up with shutdownhook

Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
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?


Thanks<br />Karthik<br />SCJP 1.4, CCNA.<br /> <br />"Success is relative. More the success, more the relatives."
 
jQuery in Action, 2nd edition
 
subject: Cleaning up with shutdownhook