Thursday, June 02, 2005

Signaling thread about JVM exit

Last nite I was trying to make a small program which works something like Stop watch, I was trying to keep watch functionality totally different from presentation, Then suddenly I needed a way to "Signal thrad about JVM exit", Not sure if in past I ever needed it, but I was literally stuck kindof for a moment before I came up with 2 workarounds
1) Check for user exit event in GUI and signal thread for stopping.
2) Write a finalize method in thread container and make sure u stop thread in finalize.

But 2nd step will not work in scenario like JVM exit without doing any garbage collection, So leaving with only available option i.e. step 1.
But I wonder, What If you don't want to track of all the dynamic weak object references(having some worker thread in them) which are created and destroyed dynamically?
I am sure if we just exit the JVM it assures all threads are stopped, but what if you want to do something specifically only on JVM exit? I guess for this step 1 is the only way.

1 comment:

Kiran said...

have you checked out Shutdown Hooks in java. Shutdown hook are mainly used for events like JVM exit... when user abruptly exits the application etcc...

http://kiranhk.blogspot.com/2005/01/java-5-feature-shutdown-hook.html