File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cleaning up with shutdownhook

 
Karthik Veeramani
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic