System.exit

R

Roedy Green

The docs say System.exit terminates the current JVM. But it seems
that when you have other threads executing, they keep on going.
--
Roedy Green Canadian Mind Products
http://mindprod.com
For me, the appeal of computer programming is that
even though I am quite a klutz,
I can still produce something, in a sense
perfect, because the computer gives me as many
chances as I please to get it right.
 
E

Eric Sosman

The docs say System.exit terminates the current JVM. But it seems
that when you have other threads executing, they keep on going.

Evidence? Better, yet, SSCCE?
 
A

Andreas Leitgeb

Roedy Green said:
The docs say System.exit terminates the current JVM. But it seems
that when you have other threads executing, they keep on going.

Are you sure you really do System.exit(), rather than just
returning from the main() method? In the latter case, all
those threads *not* marked as daemon threads keep running.
 
D

Daniel Pitts

Are you sure you really do System.exit(), rather than just
returning from the main() method? In the latter case, all
those threads *not* marked as daemon threads keep running.
I would think even daemon threads would keep running until the last
non-daemon thread completed.
 
A

Andreas Leitgeb

Daniel Pitts said:
I would think even daemon threads would keep running until the last
non-daemon thread completed.

You're right - I had actually intended to write:
"... keep *the application* running."
Don't know, where those two words went... ;)
 
R

Roedy Green

Are you sure you really do System.exit()

I do a real system exit.
--
Roedy Green Canadian Mind Products
http://mindprod.com
For me, the appeal of computer programming is that
even though I am quite a klutz,
I can still produce something, in a sense
perfect, because the computer gives me as many
chances as I please to get it right.
 
D

Daniel Pitts

pittsdosx:noisebridge pittsd$ cat ExitTest.java
public class ExitTest {
public static void main(String...args) throws Exception {
new Thread(new Runnable() {
public void run() {
while(true) {
System.out.println("In thread!");
try {
Thread.sleep(250);
} catch (Exception e) {
e.printStackTrace();
}
}
}}).start();

Thread.sleep(2000);
System.out.println("Before exit!");
try {
System.exit(-1);
} finally { System.out.println("Finally!"); }
}
}
pittsdosx:noisebridge pittsd$ javac ExitTest.java
pittsdosx:noisebridge pittsd$ java ExitTest
In thread!
In thread!
In thread!
In thread!
In thread!
In thread!
In thread!
In thread!
Before exit!
In thread!

I see no evidence that the thread continues after System.exit() is
finished executing.

Granted, I do see a slight delay, probably due to thread scheduling,
that my log message for the "before exit" isn't the last thing output.
 
P

Paul Cager

The docs say System.exit terminates the current JVM.  But it seems
that when you have other threads executing, they keep on going.

The only time I've seen that happen is with an older version of Java 6
where the NIO "select" system call was blocking. I never investigated
what caused the problem, and I assume it is fixed now.

Is there anything trying to run because of runFinalizersOnExit()?

Any SecurityManager that might be raising an exception when you call
System.exit()?
 
R

Roedy Green


There no way I could write an SCCE with a threadpool simple enough
that anyone would read it or be convinced the problem was with
System.exit (or the thread pool manager somehow defanging
System.exit).I am just pointing this out as a warning if you mix
System.exit and threads you may get some strange behaviour. I am not
asking you to fix anything for me.

If you want to explore it, try adding a System.exit into your own code
managing a web of threads.

--
Roedy Green Canadian Mind Products
http://mindprod.com
For me, the appeal of computer programming is that
even though I am quite a klutz,
I can still produce something, in a sense
perfect, because the computer gives me as many
chances as I please to get it right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

two new JDKs 7
ant and -bootclasspath 5
Deployjava.js 2
Theads and FTP 12
Custom fonts 11
for :each style question 14
Fast String search algorithm 10
64-bit gotcha 13

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top