This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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: 14687
    
  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: 10167
    
    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: 19651
    
  18

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。
 
jQuery in Action, 2nd edition
 
subject: how to execute some procedure when vm is about to close?
 
Similar Threads
Exit Sub in java?
What is -server and -client option?
Thread Scheduling In Different JVMs
For Loop
Try/catch/finally