When do I need to call this method Runtime.getRuntime().addShutdownHook()
When do I actually need call this method Runtime.getRuntime().addShutdownHook() and when or why I need to shutdown my application. Could anyone please explain me this by giving some example.
Thanks
java
add a comment |
When do I actually need call this method Runtime.getRuntime().addShutdownHook() and when or why I need to shutdown my application. Could anyone please explain me this by giving some example.
Thanks
java
add a comment |
When do I actually need call this method Runtime.getRuntime().addShutdownHook() and when or why I need to shutdown my application. Could anyone please explain me this by giving some example.
Thanks
java
When do I actually need call this method Runtime.getRuntime().addShutdownHook() and when or why I need to shutdown my application. Could anyone please explain me this by giving some example.
Thanks
java
java
asked Jan 4 '12 at 6:21
user965884user965884
2,402146090
2,402146090
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
As far as I know, I will explain this below. You can google it and find lot of information too.
addShutdownHook()
will register some actions which is to be performed on a Program's termination. The program that you start ends in two ways:
- the main thread (Root) ends its running context;
- the program meets some unexpected situation, so it cannot proceed further.
If you add a ShutdownHook, the hook will start a thread that will start running at time of termination only. For example:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Running Shutdown Hook");
}
});
will print a Running Shutdown Hook
at the time of program termination at any point. You might even call a System.exit(0)
.
For examples, you can google, there are enough of them. And the question 'When should you use this' is like asking 'What does catch
do in a try-catch
statement'.
You might have many situations like:
- your program had created many temporary files in filesystem you want to delete it;
- you need to send a distress signal to another process/machine before terminating;
- execute any clean-up actions, logging or after-error actions on unexpected behaviours.
All this will be needed for some point of time.
For examples you can go in here Example 1 or Example 2
1
The reasons you give for termination are inaccurate. See Buhake's answer for the correct version.
– meriton
Jul 9 '15 at 11:31
add a comment |
You only worry about shutdown hooks when you want something to happen when a shutdown occurs on the virtual machine.
From Javadoc:
The Java virtual machine shuts down in response to two kinds of
events:
- The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
- The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or
system shutdown.
Thus, a shutdown hook is a initialized and unstarted thread that gets executed when a JVM shutdown occurs.
Popular examples of shutdown hooks exists in application servers (such as JBoss AS). When you press Ctrl+C, the JVM calls all the Runtime
shutdown hooks registered (such as JBoss shutdown hooks) before exiting.
but how will a Thread run whenJVM
itself is shutdowned.
– garg10may
Mar 4 '17 at 2:43
When the JVM is already shutdown, all threads will have been terminated already. I don't understand your question.
– Buhake Sindi
Mar 5 '17 at 12:00
1
@garg10may - just before the JVM shuts down, the code present within shutDownHook will be invoked. addShutDownHook method takes a thread as input argument. You can specify any tasks that you want to take care of before the final JVM exit in this thread.
– Binita Bharati
Jan 2 '18 at 7:55
Ctrl+C is a very good point.
– prageeth
Jan 29 '18 at 5:19
add a comment |
One case is, If you any daemon threads which needs to be stopped before your jvm shutdown (or) any other backend threads (mostly daemon threads) need to be gracefully exited, you will write shutdown hook and execute it using above code. Here is interesting discussion we had on SO couple of days ago. Shutdown hook
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8722826%2fwhen-do-i-need-to-call-this-method-runtime-getruntime-addshutdownhook%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
As far as I know, I will explain this below. You can google it and find lot of information too.
addShutdownHook()
will register some actions which is to be performed on a Program's termination. The program that you start ends in two ways:
- the main thread (Root) ends its running context;
- the program meets some unexpected situation, so it cannot proceed further.
If you add a ShutdownHook, the hook will start a thread that will start running at time of termination only. For example:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Running Shutdown Hook");
}
});
will print a Running Shutdown Hook
at the time of program termination at any point. You might even call a System.exit(0)
.
For examples, you can google, there are enough of them. And the question 'When should you use this' is like asking 'What does catch
do in a try-catch
statement'.
You might have many situations like:
- your program had created many temporary files in filesystem you want to delete it;
- you need to send a distress signal to another process/machine before terminating;
- execute any clean-up actions, logging or after-error actions on unexpected behaviours.
All this will be needed for some point of time.
For examples you can go in here Example 1 or Example 2
1
The reasons you give for termination are inaccurate. See Buhake's answer for the correct version.
– meriton
Jul 9 '15 at 11:31
add a comment |
As far as I know, I will explain this below. You can google it and find lot of information too.
addShutdownHook()
will register some actions which is to be performed on a Program's termination. The program that you start ends in two ways:
- the main thread (Root) ends its running context;
- the program meets some unexpected situation, so it cannot proceed further.
If you add a ShutdownHook, the hook will start a thread that will start running at time of termination only. For example:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Running Shutdown Hook");
}
});
will print a Running Shutdown Hook
at the time of program termination at any point. You might even call a System.exit(0)
.
For examples, you can google, there are enough of them. And the question 'When should you use this' is like asking 'What does catch
do in a try-catch
statement'.
You might have many situations like:
- your program had created many temporary files in filesystem you want to delete it;
- you need to send a distress signal to another process/machine before terminating;
- execute any clean-up actions, logging or after-error actions on unexpected behaviours.
All this will be needed for some point of time.
For examples you can go in here Example 1 or Example 2
1
The reasons you give for termination are inaccurate. See Buhake's answer for the correct version.
– meriton
Jul 9 '15 at 11:31
add a comment |
As far as I know, I will explain this below. You can google it and find lot of information too.
addShutdownHook()
will register some actions which is to be performed on a Program's termination. The program that you start ends in two ways:
- the main thread (Root) ends its running context;
- the program meets some unexpected situation, so it cannot proceed further.
If you add a ShutdownHook, the hook will start a thread that will start running at time of termination only. For example:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Running Shutdown Hook");
}
});
will print a Running Shutdown Hook
at the time of program termination at any point. You might even call a System.exit(0)
.
For examples, you can google, there are enough of them. And the question 'When should you use this' is like asking 'What does catch
do in a try-catch
statement'.
You might have many situations like:
- your program had created many temporary files in filesystem you want to delete it;
- you need to send a distress signal to another process/machine before terminating;
- execute any clean-up actions, logging or after-error actions on unexpected behaviours.
All this will be needed for some point of time.
For examples you can go in here Example 1 or Example 2
As far as I know, I will explain this below. You can google it and find lot of information too.
addShutdownHook()
will register some actions which is to be performed on a Program's termination. The program that you start ends in two ways:
- the main thread (Root) ends its running context;
- the program meets some unexpected situation, so it cannot proceed further.
If you add a ShutdownHook, the hook will start a thread that will start running at time of termination only. For example:
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("Running Shutdown Hook");
}
});
will print a Running Shutdown Hook
at the time of program termination at any point. You might even call a System.exit(0)
.
For examples, you can google, there are enough of them. And the question 'When should you use this' is like asking 'What does catch
do in a try-catch
statement'.
You might have many situations like:
- your program had created many temporary files in filesystem you want to delete it;
- you need to send a distress signal to another process/machine before terminating;
- execute any clean-up actions, logging or after-error actions on unexpected behaviours.
All this will be needed for some point of time.
For examples you can go in here Example 1 or Example 2
edited Jun 3 '13 at 9:55
effeffe
2,26531538
2,26531538
answered Jan 4 '12 at 6:40
KrisKris
3,35022340
3,35022340
1
The reasons you give for termination are inaccurate. See Buhake's answer for the correct version.
– meriton
Jul 9 '15 at 11:31
add a comment |
1
The reasons you give for termination are inaccurate. See Buhake's answer for the correct version.
– meriton
Jul 9 '15 at 11:31
1
1
The reasons you give for termination are inaccurate. See Buhake's answer for the correct version.
– meriton
Jul 9 '15 at 11:31
The reasons you give for termination are inaccurate. See Buhake's answer for the correct version.
– meriton
Jul 9 '15 at 11:31
add a comment |
You only worry about shutdown hooks when you want something to happen when a shutdown occurs on the virtual machine.
From Javadoc:
The Java virtual machine shuts down in response to two kinds of
events:
- The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
- The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or
system shutdown.
Thus, a shutdown hook is a initialized and unstarted thread that gets executed when a JVM shutdown occurs.
Popular examples of shutdown hooks exists in application servers (such as JBoss AS). When you press Ctrl+C, the JVM calls all the Runtime
shutdown hooks registered (such as JBoss shutdown hooks) before exiting.
but how will a Thread run whenJVM
itself is shutdowned.
– garg10may
Mar 4 '17 at 2:43
When the JVM is already shutdown, all threads will have been terminated already. I don't understand your question.
– Buhake Sindi
Mar 5 '17 at 12:00
1
@garg10may - just before the JVM shuts down, the code present within shutDownHook will be invoked. addShutDownHook method takes a thread as input argument. You can specify any tasks that you want to take care of before the final JVM exit in this thread.
– Binita Bharati
Jan 2 '18 at 7:55
Ctrl+C is a very good point.
– prageeth
Jan 29 '18 at 5:19
add a comment |
You only worry about shutdown hooks when you want something to happen when a shutdown occurs on the virtual machine.
From Javadoc:
The Java virtual machine shuts down in response to two kinds of
events:
- The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
- The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or
system shutdown.
Thus, a shutdown hook is a initialized and unstarted thread that gets executed when a JVM shutdown occurs.
Popular examples of shutdown hooks exists in application servers (such as JBoss AS). When you press Ctrl+C, the JVM calls all the Runtime
shutdown hooks registered (such as JBoss shutdown hooks) before exiting.
but how will a Thread run whenJVM
itself is shutdowned.
– garg10may
Mar 4 '17 at 2:43
When the JVM is already shutdown, all threads will have been terminated already. I don't understand your question.
– Buhake Sindi
Mar 5 '17 at 12:00
1
@garg10may - just before the JVM shuts down, the code present within shutDownHook will be invoked. addShutDownHook method takes a thread as input argument. You can specify any tasks that you want to take care of before the final JVM exit in this thread.
– Binita Bharati
Jan 2 '18 at 7:55
Ctrl+C is a very good point.
– prageeth
Jan 29 '18 at 5:19
add a comment |
You only worry about shutdown hooks when you want something to happen when a shutdown occurs on the virtual machine.
From Javadoc:
The Java virtual machine shuts down in response to two kinds of
events:
- The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
- The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or
system shutdown.
Thus, a shutdown hook is a initialized and unstarted thread that gets executed when a JVM shutdown occurs.
Popular examples of shutdown hooks exists in application servers (such as JBoss AS). When you press Ctrl+C, the JVM calls all the Runtime
shutdown hooks registered (such as JBoss shutdown hooks) before exiting.
You only worry about shutdown hooks when you want something to happen when a shutdown occurs on the virtual machine.
From Javadoc:
The Java virtual machine shuts down in response to two kinds of
events:
- The program exits normally, when the last non-daemon thread exits or when the exit (equivalently, System.exit) method is invoked, or
- The virtual machine is terminated in response to a user interrupt, such as typing ^C, or a system-wide event, such as user logoff or
system shutdown.
Thus, a shutdown hook is a initialized and unstarted thread that gets executed when a JVM shutdown occurs.
Popular examples of shutdown hooks exists in application servers (such as JBoss AS). When you press Ctrl+C, the JVM calls all the Runtime
shutdown hooks registered (such as JBoss shutdown hooks) before exiting.
answered Jan 4 '12 at 6:29
Buhake SindiBuhake Sindi
71.6k24146205
71.6k24146205
but how will a Thread run whenJVM
itself is shutdowned.
– garg10may
Mar 4 '17 at 2:43
When the JVM is already shutdown, all threads will have been terminated already. I don't understand your question.
– Buhake Sindi
Mar 5 '17 at 12:00
1
@garg10may - just before the JVM shuts down, the code present within shutDownHook will be invoked. addShutDownHook method takes a thread as input argument. You can specify any tasks that you want to take care of before the final JVM exit in this thread.
– Binita Bharati
Jan 2 '18 at 7:55
Ctrl+C is a very good point.
– prageeth
Jan 29 '18 at 5:19
add a comment |
but how will a Thread run whenJVM
itself is shutdowned.
– garg10may
Mar 4 '17 at 2:43
When the JVM is already shutdown, all threads will have been terminated already. I don't understand your question.
– Buhake Sindi
Mar 5 '17 at 12:00
1
@garg10may - just before the JVM shuts down, the code present within shutDownHook will be invoked. addShutDownHook method takes a thread as input argument. You can specify any tasks that you want to take care of before the final JVM exit in this thread.
– Binita Bharati
Jan 2 '18 at 7:55
Ctrl+C is a very good point.
– prageeth
Jan 29 '18 at 5:19
but how will a Thread run when
JVM
itself is shutdowned.– garg10may
Mar 4 '17 at 2:43
but how will a Thread run when
JVM
itself is shutdowned.– garg10may
Mar 4 '17 at 2:43
When the JVM is already shutdown, all threads will have been terminated already. I don't understand your question.
– Buhake Sindi
Mar 5 '17 at 12:00
When the JVM is already shutdown, all threads will have been terminated already. I don't understand your question.
– Buhake Sindi
Mar 5 '17 at 12:00
1
1
@garg10may - just before the JVM shuts down, the code present within shutDownHook will be invoked. addShutDownHook method takes a thread as input argument. You can specify any tasks that you want to take care of before the final JVM exit in this thread.
– Binita Bharati
Jan 2 '18 at 7:55
@garg10may - just before the JVM shuts down, the code present within shutDownHook will be invoked. addShutDownHook method takes a thread as input argument. You can specify any tasks that you want to take care of before the final JVM exit in this thread.
– Binita Bharati
Jan 2 '18 at 7:55
Ctrl+C is a very good point.
– prageeth
Jan 29 '18 at 5:19
Ctrl+C is a very good point.
– prageeth
Jan 29 '18 at 5:19
add a comment |
One case is, If you any daemon threads which needs to be stopped before your jvm shutdown (or) any other backend threads (mostly daemon threads) need to be gracefully exited, you will write shutdown hook and execute it using above code. Here is interesting discussion we had on SO couple of days ago. Shutdown hook
add a comment |
One case is, If you any daemon threads which needs to be stopped before your jvm shutdown (or) any other backend threads (mostly daemon threads) need to be gracefully exited, you will write shutdown hook and execute it using above code. Here is interesting discussion we had on SO couple of days ago. Shutdown hook
add a comment |
One case is, If you any daemon threads which needs to be stopped before your jvm shutdown (or) any other backend threads (mostly daemon threads) need to be gracefully exited, you will write shutdown hook and execute it using above code. Here is interesting discussion we had on SO couple of days ago. Shutdown hook
One case is, If you any daemon threads which needs to be stopped before your jvm shutdown (or) any other backend threads (mostly daemon threads) need to be gracefully exited, you will write shutdown hook and execute it using above code. Here is interesting discussion we had on SO couple of days ago. Shutdown hook
edited May 23 '17 at 12:09
Community♦
11
11
answered Jan 4 '12 at 6:31
kosakosa
59k9108146
59k9108146
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8722826%2fwhen-do-i-need-to-call-this-method-runtime-getruntime-addshutdownhook%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown