Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Making sure a method is called when an application exits

Posted on 2004-10-14
5
Medium Priority
?
271 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 600 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

927 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