File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to execute some procedure when vm is about to close? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to execute some procedure when vm is about to close?" Watch "how to execute some procedure when vm is about to close?" New topic
Author

how to execute some procedure when vm is about to close?

jing hu
Ranch Hand

Joined: Jun 30, 2009
Posts: 63


How can implement this.
Any comments will be popular.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I think you are looking for something like java.lang.Runtime#addShutdownHook(Thread hook).


[My Blog]
All roads lead to JavaRanch
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

....and welcome to the Ranch


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
jing hu
Ranch Hand

Joined: Jun 30, 2009
Posts: 63
Christophe Verré wrote:I think you are looking for something like java.lang.Runtime#addShutdownHook(Thread hook).

Thank you very much.
It is just what i want.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Just be warned that those shutdown hooks are only called when the JVM is exiting properly, by having all non-daemon threads closed (including letting the main method end), calling System.exit(...), or closing a windowed application from the Windows task manager's first tab.

If your JVM is forcefully terminated, by a crash (e.g. caused by a bug in native code) or by terminating the process from the Windows task manager's second tab, it stops right there and then and no shutdown hooks are called.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
jing hu
Ranch Hand

Joined: Jun 30, 2009
Posts: 63
Rob Prime wrote:Just be warned that those shutdown hooks are only called when the JVM is exiting properly, by having all non-daemon threads closed (including letting the main method end), calling System.exit(...), or closing a windowed application from the Windows task manager's first tab.

If your JVM is forcefully terminated, by a crash (e.g. caused by a bug in native code) or by terminating the process from the Windows task manager's second tab, it stops right there and then and no shutdown hooks are called.


yeal I get what you say.
this will not have serious impact on my program.
Anyway, Thank you for you remind。
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to execute some procedure when vm is about to close?