When does the JVM exit? 1)After the main method returns. 2)After all the non demons threads created by the application complete. 3)After all the demon threads created by the application complete 4)When a thread executes System.exit(); 5)When an uncaught exception is thrown in a non demon thread. 6)When an uncaught exception is thrown in a demon thread.
The anwser is 2),4),5),6) I understand 2),but I have doubt in 4). I am not clear about 5) and 6) Could anyone explain this for me? Thx --------------------------------------------------------------------------------
Fred Abbot
Ranch Hand
Joined: Jun 01, 2000
Posts: 300
posted
0
well system.exit(0) does just that exits so thats done an uncaught exception always stops the program since it can not go any further as you didnt tel it what to do
Hemant Bhaiya
Greenhorn
Joined: Nov 01, 2000
Posts: 21
posted
0
Hello Bin, Only answers 2 and 4 are correct. I have written a code to demonstrate this: public class Wrap { public static void main(String[] args) { th t = new th();
Thread tt = new Thread(t); tt.start();
Thread tt1 = new Thread(t); tt1.start(); } } class th implements Runnable { int i = 0; public void run() { i++; if(i==1) loop(); else loop1(); }
About 4 :As shown in the code,when you call System.exit(0) in any thread,the JVM exits (Check the output stops there). About 5 & 6 : They are not correct,if an uncaught exception arises in any thread then only that thread throws the exceotion and stops processing but other thread are still working(To check this,in above code,comment the System.exit(0) line and uncomment the originally commented line).You will see an exception is raised and that thread stops processing but other thread still shows output. Hope you are clear. Regards, Hemant
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.