Solved

Making sure a method is called when an application exits

Posted on 2004-10-14
5
241 Views
Last Modified: 2010-03-31
I have a question about how Java works. I know you can have a finalize method to call when an object is being garbage collected to free up some resources to avoid a memory leak.

However, let us assume I have a program as such,

public class Test {

   public static void main (String [] args) {

      while (true) {

         ...
      }
   }
}

that design or a server design where the server waits for a connection forever until it is shutdown by the user. So, when I run this program I have to shut it down by using a Ctrl + C key. Is there a way I can make sure a method is called before the program exits so that I can free up some or call some required resources?

Thanks and if I get a great answer (that solves this problem), I'll definitely add extra points.

Thanks

AJ
0
Comment
Question by:aratani
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 12311641
Don't use finalizers - unreliable. Use shutdown hook:

http://java.sun.com/developer/TechTips/2000/tt0711.html
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12311876
you can also have a finally block after a try block. this makes sure that whatever hapens the code in the finally block will be called always before exiting.
0
 
LVL 4

Author Comment

by:aratani
ID: 12312316
A finally block is NOT called if you exit the program by using Ctrl + C or through the task bar.

AJ
0
 
LVL 4

Author Comment

by:aratani
ID: 12312918
Thanks CEHJ, the shutdown hook worked perfectly. I increased the points and am accepting your result.

AJ
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12313662
:-)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 99
Where to store the queries for modification of table 4 63
print map entry 34 69
CSV file parsing thru Java 13 31
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question