The given java code is not going to the finally block, I thought these blocks were supposed to execute no matter what:
public static void main(String[] args) {
try {
System.out.println("Hello world");
System.exit(0);
} finally {
System.out.println("Goodbye world");
}
}
System.exit(0);
will unload the JVM i.e no further java instructions are processed
That is the reason for not being exicuting the finally{}
As stated in the Java 6 System.exit() docs:
The call System.exit(n) is effectively equivalent to the call: Runtime.getRuntime().exit(n)
And, if you go and look at Runtime.exit() (my bold):
Terminates the currently running Java virtual machine by initiating its shutdown sequence. This method never returns normally.
The virtual machine's shutdown sequence consists of two phases. In the first phase all registered shutdown hooks, if any, are started in some unspecified order and allowed to run concurrently until they finish. In the second phase all uninvoked finalizers are run if finalization-on-exit has been enabled. Once this is done the virtual machine halts.
Basically, the only one this function can return (and hence allow the finally clause to run) is for it to raise a SecurityException because whatever security manager is running disallows exiting with the given code.
Yes, that's normal. The finally blocks is always executed, except in the case where the JVM is stopped before reaching the end of the code, which is your case here, as you exit the JVM.
The System.exit method stops the execution of the current thread and all others threads.
The presence of a finally does not give a thread special permission to continue executing.
A previous discusses this in great detail.
How does Java's System.exit() work with try/catch/finally blocks?
By System.exit(0) You are exiting from Jvm so no lines after this will get executed and That's why you are finding your finally block as unexecuted.
Related
This question already has answers here:
Does a finally block always get executed in Java?
(51 answers)
Closed 7 years ago.
So I've been assigned to teach a block on exception handling, and I've run into a question I don't have an answer for. What valid (e.g., don't result in either a compiler, or a run-time, error) commands in Java will keep a finally-block from executing?
A return statement won't do it, but a System.exit(0) statement will. My understanding is that a finally-block would execute no matter what. What (valid) statements will prevent a finally-block from doing so, and why?
Quoting Docs:
If the JVM exits while the try or catch code is being executed,
then the finally block may not execute. Likewise, if the thread
executing the try or catch code is interrupted or killed, the finally
block may not execute even though the application as a whole
continues.
Regarding your example about System.exit(), quoting Docs again
Terminates the currently running Java Virtual Machine. The argument
serves as a status code; by convention, a nonzero status code
indicates abnormal termination.
which means that finally will not be executed.
Simple example:
try {
System.out.println("try");
System.exit(0);
} finally {
System.out.println("finally");
}
The above example will print only try but not finally
To quote Oracle's tutorials:
Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.
System.exit will just quite the JVM, thus preventing the finally block from running, as will killing the thread containing that block from another thread. Another edge case is having an infinite loop in the try block, which will simply prevent it from ending, so the finally loop will never be reached.
I see three cases to never reach finally block (or any further instructions in the stack frame):
Sudden thread death (including JVM exit)
stack frame operations
Infinite loop ; whatever it's active (while (true)) or passive (acquiring lock, waiting for notification, etc.)
Only native code (including own JVM mechanisms) is supposed to kill thread or play with stack frames. Fatal system errors are also an option.
However, JVM implementations are free to offer such (undesirable ?) feature across his specific API (ie sun.misc.Unsafe) or via an ugly implementation of Java SE API (ie Thread.destroy)
What happens when the JVM is terminated with System.exit(0) or ^C or anything of that kind? I read things like "the process is just blown away" and "every single thread is stopped", but I would like to know what happens exactly. I already know there is the shutdownHook that somehow still gets executed, but what happens before the shutdownHooks are invoked and does anything happen after all these threads have finished?
I would like to implement such a shutdownHook correctly and to do so I need to make the right assumptions about what might still be executed and what will not.
update:
some code:
class SomeObject {
private boolean stopped;
SomeObject() {
stopped = false;
Thread hook = new Thread() {
#Override
public void run() {
stopped = true;
}
};
hook.setPriority(Thread.MAX_PRIORITY);
Runtime.getRuntime().addShutdownHook(hook);
}
boolean map(Iterator<Object> it) {
while(it.hasNext() && !stopped) {
writeToOtherObject(it.next());
it.remove();
}
//have calculations finished?
return !it.hasNext();
}
}
The map function computes results that are gathered in some other object. This object should be stored in some file before everything is broken down (by normal-priority shutdownHooks too). Does the shutdownHook here make sense? As far I understand it, all threads are destroyed first and only then the shutdownHooks are run (concurrently, but I assume high-priority threads are run first...) and then objects are finalized. This makes the code above rather useless, because the intention of this shutdownHook would be to make sure no new loop is started when the shutdown has already started. Is my understanding correct and complete?
Let's begin from the different ways the shutdown sequence can be initiated:
The last non-daemon thread ends.
The JVM is interrupted (by using ctrlC or sending SIGINT).
The JVM is terminated (by sending SIGTERM)
One of the threads calls System.exit() or Runtime.exit().
When System.exit(int) is called, it calls Runtime.exit(). It checks with the security manager whether it is permitted to exit with the given status, and if so, calls Shutdown.exit().
If you interrupted the JVM or the system sent it the TERM signal, then by default, Shutdown.exit() is called directly without checking with the security manager.
The Shutdown class is an internal, package-private class in java.lang. It has, among others, an exit() and a halt() methods. Its exit() method does some stuff to prevent the hooks from being executed twice, and so on, but basically, what it does is
Run the System Hooks. The System Hooks are registered internally by JRE methods. They are ran sequentially, not in threads. The second system hook is what runs the application hooks that you have added. It starts each of them as a thread and then has a join for each of them at the end. Other system hooks may run before or after the application hooks.
If finalizers are supposed to be ran prior to halting, they are ran. This should generally not even happen, as the method has been deprecated. And if the exit is with a status other than zero, it ignores the runFinalizersOnExit anyway.
The JVM is halted.
Now, contrary to your supposition, it is at stage 3 that all the threads are stopped. The halt method is native, and I have not attempted to read the native code, but up to the moment it is called, the only code being ran is pure Java, and there is nothing that stops the threads anywhere in it. The documentation of Runtime.addShutdownHook says, in fact:
A shutdown hook is simply an initialized but unstarted thread. When the virtual machine begins its shutdown sequence it will start all registered shutdown hooks in some unspecified order and let them run concurrently. When all the hooks have finished it will then run all uninvoked finalizers if finalization-on-exit has been enabled. Finally, the virtual machine will halt. Note that daemon threads will continue to run during the shutdown sequence, as will non-daemon threads if shutdown was initiated by invoking the exit method.
(emphasis mine)
So you see, it is indeed part of the shutdown hook's job to tell threads that they should leave their loops and clean up.
Another misconception you have is about giving the thread a high priority. A high priority doesn't mean that the thread will run first, before all other hooks. It merely means that whenever the operating system has to make a decision which of the threads which are in "ready to run" state to give to a CPU to run, a high-priority thread will have a higher probability of "winning" - depending on the operating system's scheduling algorithm. In short, it may get a little more CPU access, but it will not - especially if you have more than one CPU core - necessarily start before other threads or complete before them.
One last thing - if you want to use a flag to tell a thread to stop working, that flag should be volatile.
Take a look at this article from DZone. It covers the JVM and its termination in sufficient detail, with a focus on the use of ShutdownHook.
From a high level, some of the important assumptions it covers include:
Shutdown Hooks may not be executed in some cases!
Once started, Shutdown Hooks can be forcibly stopped before completion.
You can have more than one Shutdown Hook, but their execution order is not guaranteed.
You cannot register / unregister Shutdown Hooks with in Shutdown Hooks
Once shutdown sequence starts, it can be stopped by Runtime.halt() only.
Using shutdown hooks require security permissions.
Exceptions thrown by the Shutdown Hooks are treated same as exceptions thrown by any other code segment.
I'm running my Java application in cmd.exe in Windows. If I stop the process forcefully by pressing Ctrl-C, and the code at that moment was running in the try block, will the finally block still be executed?
In my tests it seems that, yes, it is executed.
The correct way to ensure that some code is run in response to an operating system signal (which is what Ctrl-C does, it sends a SIGINT) is to register a "shutdownHook". Here's a StackOverflow question about handling it, and here's an article with way more detail about the JVM's signal handling than you probably will ever want to know.
While your finally code may have executed on your Windows machine (I couldn't reproduce it with Linux), according to this documentation on finally:
Note: If the JVM exits while the try
or catch code is being executed, then
the finally block may not execute.
Likewise, if the thread executing the
try or catch code is interrupted or
killed, the finally block may not
execute even though the application as
a whole continues.
So I wouldn't use a finally block to make sure a piece of code executes, even if the user tries to prematurely exit. If you need that, you can use, like Adrian Petrescu mentioned, Shutdown Hooks
In my test on Windows 7, Sun Java 1.6, the finally block did not execute if I pressed Ctrl-C during this try block.
public static void main(String[] args) {
try {
for (int i = 0; i < 1000; i++) {
System.out.println(i);
}
}
finally {
System.out.println("finally");
}
}
Depending on OS implementations, generally sigkills are meant to stop applications immediately. In this situation it is not desirable to rely on finally block to execute; it may in some situations, but generally it would/should not. The java.lang.Runtime documentation further supports this:
In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.
No, the finally block is not being run for me when I Ctrl-C (in Linux).
I recommend moving your finally logic to a method "shutdown". Then calling it from both the finally block as well as from a Ctrl-C handler. Like this:
private void registerSigIntHandler() {
final SignalHandler defaultSigIntHandler = Signal.handle(new Signal("INT"), SignalHandler.SIG_DFL);
Signal.handle(new Signal("INT"), new SignalHandler() {
public void handle(Signal signal) {
log.info("SIGINT received");
Signal.handle(new Signal("INT"), SignalHandler.SIG_DFL);
shutdown();
defaultSigIntHandler.handle(signal); }
});
}
The benefit of doing it this way is that after shutdown completes, the SIG_DFL handler executes, terminating the program via the default OS SIGINT handler. This terminates the main thread and the entire process. Otherwise, if you do not use SIG_DFL, you have to somehow signal the main thread to exit. My main thread does not listen/poll for shutdown signals/messages. It is a long-running data-migration job. I want to shut down certain things but terminate the main thread and subthreads.
Also if shutdown hangs (hopefully should not happen), a second ctrl-c will terminate the program via the SIG_DFL handler.
In shutdown, I also set an AtomicBoolean to ensure shutdown is only run once.
boolean isShuttingDown = this.isShuttingDown.getAndSet(true);
if (isShuttingDown) {
return;
}
I also tried addShutdownHook (an alternative to Signal.handle) but ran into trouble with it not running my shutdown routine fully. I was messaging and joining with subthreads.
Note: Do not call SignalHandler.SIG_DFL.handle(new Signal("INT"));. It gives me SIGSEGV every time (which does exit the program). - How do I trigger the default signal handling behavior?
I have a call to System.exit(1) in my multi-threaded program. However from time to time instead of return code 1, the program exits with return code 0. I don't have any other calls System.exit() and I'm positive that the program doesn't exit cleanly. What can be the cause, and how can I avoid it?
Note that the error is intermittent and I cannot reproduce the same behavior in single threaded programs.
Modify your design to execute a more controlled shutdown.
There should be no expectation that calling System.exit() in an application with multiple threads would ever cause the program to exit cleanly.
Rather than calling System.exit() to leave the program, you should send shutdown messages to each moving component and use Thread.join() to recover any threads you created. Your application should be able to shut down all pieces nicely this way. The final command in the main thread should be to return your exit code. If you just call System.exit(), you're leaving all of these shut down details to the JVM, which is just going to take a heavy-handed approach and kill everything on the spot.
Have you used Runtime.getRuntime.addShutdownHook() at all? A call to System.exit() will invoke any shutdown hooks that may be installed and this could be changing the exit code.
The documentation for Runtime.halt(int) says the following about its argument:
If the exit (equivalently, System.exit) method has already been invoked then this status code will override the status code passed to that method.
So perhaps something is invoking Runtime.halt(int). In a shutdown hook or finalizer?
I think the only way this may happen if your JVM terminates before System.exit(1) actually executed. Do you think this may be possible in your system?
Either, the code with System.exit(1) is being executed in a daemon thread, and so when all other live (non-daemon) threads finish working JVM exits cleanly (or not cleanly, as you can still get 0 exit code if you program throws an exception!)
Alternatively, as #Erick Robertson suggested, maybe something is modifying the exit status from a hook or something, although I am not sure how this is possible.
Note: please disregard my previous comment. Calling System.exit(1) will terminate all currently running daemon/non-daemon threads all-together.
I have a Java program which is being started via ProcessBuilder from another Java program.
System.exit(0) is called from the child program, but for some of our users (on Windows) the java.exe process associated with the child doesn't terminate. The child program has no shutdown hooks, nor does it have a SecurityManager which might stop System.exit() from terminating the VM. I can't reproduce the problem myself on Linux or Windows Vista. So far, the only reports of the problem come from two Windows XP users and one Vista user, using two different JREs (1.6.0_15 and 1.6.0_18), but they're able to reproduce the problem every time.
Can anyone suggest reasons why the JVM would fail to terminate after System.exit(), and then only on some machines?
Edit 1: I got the user to install the JDK so we could get a thread dump from the offending VM. What the user told me is that the VM process disappears from VisualVM as soon as he clicks on the 'Quit' item in my menu---but, according to Windows Task Manager, the process hasn't terminated, and no matter how long the user waits (minutes, hours), it never terminates.
Edit 2: I have confirmed now that Process.waitFor() in the parent program never returns for at least one of the users having the problem. So, to summarize: The child VM seems to be dead (VisualVM doesn't even see it) but the parent still sees the process as live and so does Windows.
This can happen if your code (or a library you use) has a shutdown hook or a finalizer that doesn't finish cleanly.
A more vigorous (so should only be used in extreme cases!) way to force shutdown is by running:
Runtime.getRuntime().halt(0);
The parent process has one thread
dedicated to consuming each of the
child's STDOUT and STDERR (which
passes that output through to a log
file). So far as I can see, those are
working properly, since we're seeing
all the output we expect to see in the
log
i had a similar problem with my program not disappearing from task mgr when i was consuming the stdout/stderr. in my case, if I closed the stream that was listening before calling system.exit() then the javaw.exe hung around. strange, it wasn't writing to the stream...
the solution in my case was to simply flush the stream rather than close it before existing. of course, you could always flush and then redirect back to stdout and stderr before exit.
Check if there is a deadlock.
For example,
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
System.exit(1);
and in the close()
public void close() {
// some code
System.exit(1);
}
The System.exit() actually calls the shutdown hook and finalizers. So, if your shutdown hook calls the exit() again internally, for some reason (for example, when the close() raises an exception for which you want to exit the program, then you will call exit() there also). Like this..
public void close() {
try {
// some code that raises exception which requires to exit the program
} catch(Exception exceptionWhichWhenOccurredShouldExitProgram) {
log.error(exceptionWhichWhenOccurredShouldExitProgram);
System.exit(1);
}
}
Though, it is a good practice to throw the exception, some may choose to log and exit.
Note, also, that Ctrl+C will also not work if there is a deadlock.
Since it also calls the shutdown hook.
Anyways, if it is the case, the problem can be solved by this workaround:
private static AtomicBoolean exitCalled=new AtomicBoolean();
private static void exit(int status) {
if(!exitCalled.get()) {
exitCalled.set(true);
System.exit(status);
}
}
Runtime.getRuntime().addShutdownHook(new Thread(MyClass::close));
exit(1);
}
private static void close() {
exit(1);
}
P.S: I feel that the above exit() version must actually be written
in the System.exit() method only (may be some PR for JDK?) Because,
there is practically no point (at least from what I see) in
entertaining a deadlock in System.exit()
Here are a couple of scenarios...
Per the definition of a Thread in http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html
...
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:
1) The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
2) All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
Another possibility is if the method runFinalizersOnExit has been called. as per the documentation in http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html
Deprecated. This method is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock.
Enable or disable finalization on exit; doing so specifies that the finalizers of all objects that have finalizers that have not yet been automatically invoked are to be run before the Java runtime exits. By default, finalization on exit is disabled.
If there is a security manager, its checkExit method is first called with 0 as its argument to ensure the exit is allowed. This could result in a SecurityException.
Maybe a badly written finalizer? A shutdown hook was my first thought when I read the subject line. Speculation: would a thread that catches InterruptedException and keeps on running anyway hold up the exit process?
It seems to me that if the problem is reproducible, you should be able to attach to the JVM and get a thread list/stack trace that shows what is hung up.
Are you sure that the child is still really running and that it's not just an unreaped zombie process?
Does the parent process consumes the error- and outputstream from the child process?
If under some OS the childprocess print out some errors/warning on stdout/stderr and the parent process is not consuming the streams, the childprocess will block and not reach System.exit();
Hy i had the same problem, but the couse for me was i was using the remote Debugging(VmArgs: -Xdebug -Xrunjdwp:transport=dt_socket,address=%port%,server=y,suspend=y) when i disabled this the java.exe Process exited as expected.
I think all of the obvious causes have been provisionally covered; e.g. finalizers, shutdown hooks, not correctly draining standard output / standard error in the parent process. You now need more evidence to figure what is going on.
Suggestions:
Set up a Windows XP or Vista machine (or virtual), install the relevant JRE and your app, and try to reproduce the problem. Once you can reproduce the problem, either attach a debugger or send the relevant signal to get a thread dump to standard error.
If you cannot reproduce the problem as above, get one of your users to take a thread dump, and forward you the log file.
Another case not mentioned here is if the shutdown hooks hang on something so be careful in writing the shutdown hook code(and if a 3rd party library registered a shutdown hook that is hanging as well).
Dean