Both cause a program to stop executing. It’s clear that there must be some differences in how this happens, though. What are they?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Summary
thread.interrupt()does not stop a thread. It is used for coordination in multi-threaded programs. Don’t use it unless you know exactly what you do.RuntimeExceptionwill (usually) terminate the thread but not necessarily the program.System.exit(int)almost always terminates the program and returns a status code.System.exit(int)might not actually stop the program.Runtime.getRuntime().halt(int)on the other hand, always does.Thread Interruption
I’m afraid your first sentence is wrong.
Thread.currentThread().interrupt()does not stop the thread or the program.Interrupting a thread is a way to signal that it should stop, but this is a cooperative effort: The code in the thread is supposed to check the interrupted status from time to time and (in most cases – but even this is only optional) should stop if is has been interrupted. If it doesn’t do that nothing will happen.
Specifically, interrupting a thread (any thread, include the currently executing one) will only set the interrupted flag. Certain methods in the standard library will throw an InterruptedException, but this is also just a way to signal that the thread has been interrupted. What should be done in such a situation is up to the code running in that thread.
Here are the relevant parts from the Java Concurrency in Practice book by Brian Goetz:
Exceptions and System.exit(int)
The Javadoc of
System.exit(int)says:So calling
exit()will (almost) definitely stop your program. In contrast to throwing aRuntimeException(or anError), this can not be caught somewhere down the call stack and it does also not depend on whether there are other threads running. On the other hand, an uncaught exception terminates the thread in which it was thrown, but if there are any other (non-daemon) threads, the program will continue to run.Another difference to throwing an Exception is that
exit()will not print anything to the console (as does an uncaught exception) but instead makes the program return a specific status code. Status codes are sometimes used in shell or batch scripts but other than that, they are not very useful.Runtime.halt(int)
Finally (for completeness’ sake), I’d like to point out a third possibility to exit a Java program. When
System.exit(int)is called (or the program ends in some other way), the runtime does some cleanup work before the Java Virtual Machine is halted. This is described in the Javadoc of Runtime.exit(int) (which is called bySystem.exit(int):If any shutdown hook or finalizer is prevented from completing, for example because of a deadlock, the program might never actually exit. The only method that guarantees that the JVM halts is Runtime.halt(int):