• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Cleaning up with shutdownhook

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
reply
    Bookmark Topic Watch Topic
  • New Topic