Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Using Shutdown Hook

Thread view: 
rossum - 25 Nov 2007 12:46 GMT
Thanks to all of you who responded to my thread on replacing
runFinalizersOnExit.

David Roden suggested using a Shutdown Hook.  Never having used one
before I put together a simple test program, (below), which seems to
work.  Have I made any mistakes in this?  Any pointers to improvements
would be helpful.

Thanks in advance,

rossum

// --- Begin Code ---

import java.lang.ref.WeakReference;

/**
* Testing Shutdown Hook
*/
public class HookTester {
   private double mSecret;

   public HookTester() {
       mSecret = Math.random();
       
       // Set up ShutdownHook in constructor
       ShutdownHook sdh = new ShutdownHook(
           new WeakReference<HookTester>(this));
       Runtime.getRuntime().addShutdownHook(new Thread(sdh));
   }
   
   public void dispose() {
       System.out.println("Running dispose()");
       mSecret = 0.0;
       System.out.println("Secret deleted.");
   }

   @Override
   protected void finalize() {
       System.out.println("Running finalize()");
       dispose();
   }

   class ShutdownHook implements Runnable {
       // Use a weak reference so as not to delay garbage collection.
       private WeakReference<HookTester> mWeakRef;
       
       public ShutdownHook(WeakReference<HookTester> wRef) {
           mWeakRef = wRef;
       }
       
       public void run() {
           if (mWeakRef.get() != null) {
               mWeakRef.get().dispose();
           }
       }
   }
   
   public static void main(String[] args) {
       HookTester ht = new HookTester();
       // ht.dispose() not explicitly called.
   }
}

// --- End Code ---
Mark Thornton - 25 Nov 2007 13:38 GMT
> Thanks to all of you who responded to my thread on replacing
> runFinalizersOnExit.
[quoted text clipped - 7 lines]
>
> rossum

Warning: shutdown hook doesn't work reliably in Windows applications
that have a GUI. The process can (and often does) terminate before
shutdown hooks have completed.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4486580

The shutdown hooks work if the application is closed normally or doesn't
have a GUI, but not if the close is the result of the user logging off
or shutting down the system.

Mark Thornton


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.