Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Making sure a method is called when an application exits

Posted on 2004-10-14
5
249 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
servlet doXXX methods 3 62
ejb example issues 3 26
Tagging and Merging on Branch 1 30
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)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

860 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